Collectors.groupingBy 使用
本文主要介绍一下lambda表达式中的Collectors.groupingBy的使用。
//groupingBy使用
System.out.println("=======groupingBy==========");
Stream<Person> stream = Stream.of(new Person("1", "aa", "12"), new Person("1", "bb", "13"), new Person("3", "cc", "14"));
System.out.println(stream.collect(Collectors.groupingBy(x -> x.id)));
//groupingBy
Map<String, List<Person>> tempMap = Stream.of(new Person("1", "aa", "12"), new Person("1", "bb", "13"), new Person("3", "cc", "14"))
.collect(Collectors.groupingBy(x -> x.id));
for (String s : tempMap.keySet()) {
tempMap.get(s).stream().forEach(x -> System.out.println(x));
}
Map<Boolean, List<Integer>> collectGroup = Stream.of(1, 2, 3, 4)
.collect(Collectors.groupingBy(it -> it > 3));
System.out.println("collectGroup : " + collectGroup);
class Person {
String id;
String name;
String age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Person() {
}
public Person(String id, String name, String age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"id='" + id + ''' +
", name='" + name + ''' +
", age='" + age + ''' +
'}';
}
}
看一下运行结果:

最后
以上就是紧张大门最近收集整理的关于Collectors.groupingBy 使用Collectors.groupingBy 使用的全部内容,更多相关Collectors.groupingBy内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复