java jdk1.8 新特性:stream流进行list 分组归类
```package com.springboot;
/**
* @author chenLing
* @version 0.0.1
* @description TestVo
* @since 2021/10/28 0028 21:49
*/
public class TestVO {
private String name;
private Integer old;
private String work;
@Override
public String toString() {
return "TestVO{" +
"name='" + name + '\'' +
", old=" + old +
", work='" + work + '\'' +
'}';
}
public TestVO(String name, Integer old, String work) {
this.name = name;
this.old = old;
this.work = work;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getOld() {
return old;
}
public void setOld(Integer old) {
this.old = old;
}
public String getWork() {
return work;
}
public void setWork(String work) {
this.work = work;
}
}
```java
package com.springboot;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author chenLing
* @version 0.0.1
* @description TestApp
* @since 2021/10/28 0028 21:50
*/
public class TestApp {
public static void main(String[] args) {
List<TestVO> testVOList = new ArrayList<TestVO>();
testVOList.add(new TestVO("小明",18,"student")) ;
testVOList.add( new TestVO("小明",20,"student")) ;
testVOList.add(new TestVO("小黄",20,"打铁的")) ;
testVOList.add(new TestVO("小黄",18,"打铁的")) ;
testVOList.add(new TestVO("小黑",18,"student")) ;
List<List<TestVO>> nameList = new ArrayList<>();
testVOList.stream().collect(Collectors.groupingBy(TestVO::getName,
Collectors.toList())).forEach((name,fooListByName)->{
nameList.add(fooListByName);
});
for (List<TestVO> group: nameList) {
System.out.println("nameList::"+group.size());
System.out.println("nameList::"+group);
}
List<List<TestVO>> oldList = new ArrayList<>();
testVOList.stream().collect(Collectors.groupingBy(TestVO::getName,
Collectors.toList())).forEach((name,fooListByName)->{
oldList.add(fooListByName);
});
for (List<TestVO> group: oldList) {
System.out.println("oldList::"+group.size());
System.out.println("oldList::"+group);
}
List<List<TestVO>> workList = new ArrayList<>();
testVOList.stream().collect(Collectors.groupingBy(TestVO::getName,
Collectors.toList())).forEach((name,fooListByName)->{
workList.add(fooListByName);
});
for (List<TestVO> group: workList) {
System.out.println("workList::"+group.size());
System.out.println("workList::"+group);
}
}
}
输出如下:
nameList::2
nameList::[TestVO{name='小明', old=18, work='student'}, TestVO{name='小明', old=20, work='student'}]
nameList::1
nameList::[TestVO{name='小黑', old=18, work='student'}]
nameList::2
nameList::[TestVO{name='小黄', old=20, work='打铁的'}, TestVO{name='小黄', old=18, work='打铁的'}]
oldList::2
oldList::[TestVO{name='小明', old=18, work='student'}, TestVO{name='小明', old=20, work='student'}]
oldList::1
oldList::[TestVO{name='小黑', old=18, work='student'}]
oldList::2
oldList::[TestVO{name='小黄', old=20, work='打铁的'}, TestVO{name='小黄', old=18, work='打铁的'}]
workList::2
workList::[TestVO{name='小明', old=18, work='student'}, TestVO{name='小明', old=20, work='student'}]
workList::1
workList::[TestVO{name='小黑', old=18, work='student'}]
workList::2
workList::[TestVO{name='小黄', old=20, work='打铁的'}, TestVO{name='小黄', old=18, work='打铁的'}]