代码:
package com.java;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import com.cc.MyMapImpl;
public class Test4 {
static int m=0;
public static void main(String[] args) {
Listlist=new ArrayList<>();
for(int index=0;index<15;index++) {
if(index<5) {
list.add(new Student("张三",String.valueOf(index)));
continue;
}else if(index<10) {
list.add(new Student("李四",String.valueOf(index)));
continue;
}
list.add(new Student("王五",String.valueOf(index)));
}
Map>result2=list.stream()
.collect(Collectors.groupingBy(Student::getName));
System.out.println(result2.toString());
System.out.println(list.toString());
Mapresult=list.stream()
.collect(Collectors.groupingBy(Student::getName,Collectors.counting()));
System.out.println(result.toString());
String str= list.stream()
.collect(Collectors.groupingBy(Student::getName,Collectors.counting()))
.entrySet().stream().max(Map.Entry.comparingByValue())
.map(Map.Entry::getKey)
.get();
System.out.println(str);
}
}
class Student{
private String name;
private String age;
public Student(String name,String age ) {
this.name=name;
this.age=age;
}
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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
预期效果应该是:
{张三=[Student [name=张三, age=5], Student [name=张三, age=6], Student [name=张三, age=7], Student [name=张三, age=8], Student [name=张三, age=9]], 李四=[Student [name=李四, age=0], Student [name=李四, age=1], Student [name=李四, age=2], Student [name=李四, age=3], Student [name=李四, age=4]], 王五=[Student [name=王五, age=10], Student [name=王五, age=11], Student [name=王五, age=12], Student [name=王五, age=13], Student [name=王五, age=14]]}
[Student [name=张三, age=5], Student [name=张三, age=6], Student [name=张三, age=7], Student [name=张三, age=8], Student [name=张三, age=9]], Student [name=李四, age=0], Student [name=李四, age=1], Student [name=李四, age=2], Student [name=李四, age=3], Student [name=李四, age=4], Student [name=王五, age=10], Student [name=王五, age=11], Student [name=王五, age=12], Student [name=王五, age=13], Student [name=王五, age=14]]
{张三=5, 李四=5, 王五=5}
张三
实际结果:
{李四=[Student [name=李四, age=5], Student [name=李四, age=6], Student [name=李四, age=7], Student [name=李四, age=8], Student [name=李四, age=9]], 张三=[Student [name=张三, age=0], Student [name=张三, age=1], Student [name=张三, age=2], Student [name=张三, age=3], Student [name=张三, age=4]], 王五=[Student [name=王五, age=10], Student [name=王五, age=11], Student [name=王五, age=12], Student [name=王五, age=13], Student [name=王五, age=14]]}
[Student [name=张三, age=0], Student [name=张三, age=1], Student [name=张三, age=2], Student [name=张三, age=3], Student [name=张三, age=4], Student [name=李四, age=5], Student [name=李四, age=6], Student [name=李四, age=7], Student [name=李四, age=8], Student [name=李四, age=9], Student [name=王五, age=10], Student [name=王五, age=11], Student [name=王五, age=12], Student [name=王五, age=13], Student [name=王五, age=14]]
{李四=5, 张三=5, 王五=5}
李四
分组之后数据变为了无序,预期的张三在第一个而实际却放在了第二个输出。
第一个参数:分组按照什么分类
第二个参数:分组最后用什么容器保存返回
第三个参数:按照第一个参数分类后,对应的分类的结果如何收集
其实一个参数的Collectors.groupingBy方法的 ,第二个参数默认是HashMap::new, 第三个参数收集器其实默认是Collectors.toList