jdk8(java8),list 集合 分组,stream 流处理,groupingBy 使用
为了实现 分组后排序,增加 sorted 使用,先排序在分组,就能保证 分组后排序的效果
参考
Stream流使用groupingBy+mapping实现对分组之后的对象集合转化为对象的某个属性的集合
测试代码
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Stream流使用groupingBy+mapping实现对分组之后的对象集合转化为对象的某个属性的集合
*
* @version v1.0
* @date 2021/8/16
*/
public class GroupListTest {
public static void main(String[] args) {
List<P> list = new ArrayList<>();
list.add(new P().name("你").type("女"));
list.add(new P().name("我").type("男"));
list.add(new P().name("他").type("男"));
list.add(new P().name("她").type("女"));
System.out.println("源>:"+list);
Map<String, List<P>> groupMap1 = list.stream()
.collect(Collectors.groupingBy(P::getType));
System.out.println("简单分组>:"+groupMap1);
Map<String, Set<String>> groupMap2 = list.stream()
.collect(Collectors.groupingBy(P::getType, Collectors.mapping(P::getName, Collectors.toSet())));
System.out.println("指定字段分组>:"+groupMap2);
}
public static class P{
private String name,type;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public P name(String name) { this.name = name; return this; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public P type(String type) { this.type = type; return this; }
@Override
public String toString() {
return "P{ name='" + name + "', type='" + type + "'}";
}
}
}
输出
源>:[P{ name='你', type='女'}, P{ name='我', type='男'}, P{ name='他', type='男'}, P{ name='她', type='女'}]
简单分组>:{女=[P{ name='你', type='女'}, P{ name='她', type='女'}], 男=[P{ name='我', type='男'}, P{ name='他', type='男'}]}
指定字段分组>:{女=[你, 她], 男=[我, 他]}
import java.util.*;
import java.util.stream.Collectors;
/**
* Stream流使用groupingBy+mapping实现对分组之后的对象集合转化为对象的某个属性的集合
* @author zy
* @version v1.0
* @date 2021/8/16
*/
public class GroupListTest {
public static void main(String[] args) {
List<P> list = new ArrayList<>();
list.add(new P().no(0).age(21).name("你").type("女"));
list.add(new P().no(2).age(31).name("我").type("男"));
list.add(new P().no(1).age(22).name("他").type("男"));
list.add(new P().no(null).age(30).name("她").type("女"));
list.add(new P().no(3).age(33).name("她1").type("女"));
System.out.println("源>:"+list);
Map<String, List<P>> groupMap1 = list.stream()
.collect(Collectors.groupingBy(P::getType));
System.out.println("简单分组>:"+groupMap1);
Map<String, Set<String>> groupMap2 = list.stream()
.collect(Collectors.groupingBy(P::getType, Collectors.mapping(P::getName, Collectors.toSet())));
System.out.println("指定字段分组>:"+groupMap2);
Map<String, List<P>> groupMap3 = list.stream()
.sorted(Comparator.comparing(P::getNo,Comparator.nullsLast(Integer::compareTo)))
.collect(Collectors.groupingBy(P::getType, Collectors.toList()));
System.out.println("排序后指定字段分组>:"+groupMap3);
}
public static class P{
private String name,type;
private Integer no,age;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public P name(String name) { this.name = name; return this; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public P type(String type) { this.type = type; return this; }
public void setAge(Integer age) { this.age = age; }
public Integer getAge() { return age; }
public P age(Integer age) { this.age = age; return this; }
public void setNo(Integer no) { this.no = no; }
public Integer getNo() { return no; }
public P no(Integer no) { this.no = no; return this; }
@Override
public String toString() {
return "P{ no='" + no + "', name='" + name + "', age=" + age + ", type='" + type + "'}";
}
}
}
输出
源>:[P{ no='0', name='你', age=21, type='女'}, P{ no='2', name='我', age=31, type='男'}, P{ no='1', name='他', age=22, type='男'}, P{ no='null', name='她', age=30, type='女'}, P{ no='3', name='她1', age=33, type='女'}]
简单分组>:{女=[P{ no='0', name='你', age=21, type='女'}, P{ no='null', name='她', age=30, type='女'}, P{ no='3', name='她1', age=33, type='女'}], 男=[P{ no='2', name='我', age=31, type='男'}, P{ no='1', name='他', age=22, type='男'}]}
指定字段分组>:{女=[你, 她1, 她], 男=[我, 他]}
排序后指定字段分组>:{女=[P{ no='0', name='你', age=21, type='女'}, P{ no='3', name='她1', age=33, type='女'}, P{ no='null', name='她', age=30, type='女'}], 男=[P{ no='1', name='他', age=22, type='男'}, P{ no='2', name='我', age=31, type='男'}]}