Java 8流 - 如何使用整数键获得相当于分区的()?

发布于 2025-01-29 00:56:05 字数 643 浏览 2 评论 0原文

在下面所示的代码中,p是类型peedicate< string>的谓词。

Map<Boolean, List<String>> partitioned = numbers.stream()
    .collect(Collectors.partitioningBy(p));

是否可以将boolean密钥转换为int在分区逻辑中键入而不是具有另一个流?

看起来可以通过分组来完成。

Map<String, List<String>> targetTableColumnListMap = nqColumnMapList.stream()
    .flatMap(m -> m.entrySet().stream())
    .collect(Collectors.groupingBy(
        e -> e.getKey().toUpperCase(), 
        Collectors.mapping(Map.Entry::getValue, Collectors.toList())
    ));

In the code shown below, p is a predicate of type Predicate<String>.

Map<Boolean, List<String>> partitioned = numbers.stream()
    .collect(Collectors.partitioningBy(p));

Is it possible to convert the boolean keys to the int type inside the partitioning logic rather than having another stream?

Looks like it could be done with grouping.

Map<String, List<String>> targetTableColumnListMap = nqColumnMapList.stream()
    .flatMap(m -> m.entrySet().stream())
    .collect(Collectors.groupingBy(
        e -> e.getKey().toUpperCase(), 
        Collectors.mapping(Map.Entry::getValue, Collectors.toList())
    ));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴情 2025-02-05 00:56:05

是否可以将布尔密钥转换为int在内部输入
分区逻辑

,因此您需要将列表中的值与01(例如)相关联,以将数据存储在数据库中 )基于给定谓词

为此,代替partitioningby()您可以使用collector groupingby()

Map<Integer, List<String>> partitioned = numbers.stream()
    .collect(Collectors.groupingBy(num -> predicate.test(num) ? 1 : 0));

Is it possible to convert the boolean keys to the int type inside the
partitioning logic

So you need to associate the values in a list with 0 and 1 (for instance, in order to store the data into the database) based on the given predicate.

For that, instead of partitioningBy() you can use collector groupingBy() in conjunction with the predicate.

Map<Integer, List<String>> partitioned = numbers.stream()
    .collect(Collectors.groupingBy(num -> predicate.test(num) ? 1 : 0));
夜声 2025-02-05 00:56:05

您可以将终端操作collect(collectors.tomap())通过您的谓词进行分组,而不是返回其boolean值作为密钥,您可以使用三元操作员来返回1或0。

将布尔键转换为int类型

,您可以映射到该键list 构建的String您测试了predication要处理碰撞案例,您可以将与第二个密钥关联的list添加到list对应于第一个密钥的情况下。

这是代码的片段:

//My silly test Predicate
Predicate<String> p = s -> s.length() > 4;

//Mapping the numbers list
Map<Integer, List<String>> partitioned = numbers.stream()
        .collect(Collectors.toMap(s -> p.test(s) ? 1 : 0, 
            s -> new ArrayList<>(List.of(s)), 
            (list1, list2) -> {
                list1.addAll(list2);
                return list1;
            }
        ));

这里还有一个测试代码的链接:

https://ideone.com/kd8xri < /a>

You could use the terminal operation collect(Collectors.toMap()) to group by your predicate and instead of returning its boolean value as the key you could use a ternary operator to return either 1 or 0.

to convert the boolean keys to the int type

Then, you could map to that key a List built on the String you tested the Predicate with, and to handle the colliding cases you could add the List associated to the second key into the List corresponding to the first key.

Here's a snippet of the code:

//My silly test Predicate
Predicate<String> p = s -> s.length() > 4;

//Mapping the numbers list
Map<Integer, List<String>> partitioned = numbers.stream()
        .collect(Collectors.toMap(s -> p.test(s) ? 1 : 0, 
            s -> new ArrayList<>(List.of(s)), 
            (list1, list2) -> {
                list1.addAll(list2);
                return list1;
            }
        ));

Here, there's also a link to test the code:

https://ideone.com/Kd8xrI

挽容 2025-02-05 00:56:05

假设下面的类 -

public class Employee {
    int id;
    String name;
    int age;
    String gender;
// getters n setters
}

//现在要根据性别进行分组或获取员工的计数,我们也可以使用以下代码

Map<String, Long> noOfMaleAndFemaleEmployees =
        employeeList.stream().collect(Collectors.groupingBy(Employee::getGender, Collectors.counting()));
                
System.out.println(noOfMaleAndFemaleEmployees);

System.out.println(employeeList.stream().map(Employee::getGender)
                .filter(e -> e.equalsIgnoreCase("male")).count());  

//也可以使用过滤器

System.out.println(employeeList.stream()
                .collect(Collectors.partitioningBy(e-> e.getGender().equalsIgnoreCase("Male"), 
                        Collectors.counting())));

Assuming that below class -

public class Employee {
    int id;
    String name;
    int age;
    String gender;
// getters n setters
}

// Now to group or get the count of, Employees based on Gender, we can use below codes

Map<String, Long> noOfMaleAndFemaleEmployees =
        employeeList.stream().collect(Collectors.groupingBy(Employee::getGender, Collectors.counting()));
                
System.out.println(noOfMaleAndFemaleEmployees);

System.out.println(employeeList.stream().map(Employee::getGender)
                .filter(e -> e.equalsIgnoreCase("male")).count());  

// Filters can be used as well

System.out.println(employeeList.stream()
                .collect(Collectors.partitioningBy(e-> e.getGender().equalsIgnoreCase("Male"), 
                        Collectors.counting())));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文