java老式的分組方式(對(duì)list for循環(huán)然后 if判斷?放入map)?代碼復(fù)雜,易讀性差,維護(hù)性差,故本文匯總了Stream流中的分組方法供大家參考,如對(duì)您有幫助,請(qǐng)?zhí)Ц哔F的小手點(diǎn)個(gè)贊吧,歡迎大佬留下高見(jiàn)
(以下方法默認(rèn)都是java8的方法,java9新增方法有標(biāo)注)
List<Student> studentList = Arrays.asList(
new Student(1L,"小紅","籃球","紅色",22),
new Student(2L,"小白","足球","藍(lán)色",11),
new Student(3L,"小牛","足球","黑色",33),
new Student(4L,"小羊","足球","紅色",17),
new Student(5L,"小綠","足球","藍(lán)色",24));
1.常規(guī)按對(duì)象的字段直接將對(duì)象分組
按顏色分組
Map<String, List<Student>> byColor = studentList.stream()
.collect(groupingBy(Student::getColor));
打印Map結(jié)果:
{紅色=[Student{id=1, name='小紅', hobby='籃球', color='紅色', age=22}, Student{id=4, name='小羊', hobby='足球', color='紅色', age=17}],
藍(lán)色=[Student{id=2, name='小白', hobby='足球', color='藍(lán)色', age=11}, Student{id=5, name='小綠', hobby='足球', color='藍(lán)色', age=24}],
黑色=[Student{id=3, name='小牛', hobby='籃球', color='黑色', age=33}]}
2.對(duì)分組的條件(key)做定義將對(duì)象進(jìn)行分組
按年紀(jì)是否大于十八歲分組
Map<String, List<Student>> collect1 = studentList.stream().collect(groupingBy(key -> {
if (key.getAge() > 18) {
return "成年";
} else {
return "未成年";
}
}));
打印Map結(jié)果:
{未成年=[Student{id=2, name='小白', hobby='足球', color='藍(lán)色', age=11}, Student{id=4, name='小羊', hobby='足球', color='紅色', age=17}],
成年=[Student{id=1, name='小紅', hobby='籃球', color='紅色', age=22}, Student{id=3, name='小牛', hobby='籃球', color='黑色', age=33}, Student{id=5, name='小綠', hobby='足球', color='藍(lán)色', age=24}]}
3.在分組時(shí)對(duì)數(shù)據(jù)進(jìn)行過(guò)濾
過(guò)濾掉不想要的數(shù)據(jù)時(shí),
在java8,需在分組前對(duì)集合進(jìn)行過(guò)濾,再分組。但會(huì)導(dǎo)致一類key中的元素都被過(guò)濾掉以后,分組結(jié)果不會(huì)有該key.
Map<String, List<Student>> collect = studentList.stream()
.filter(f->!"紅色".equals(f.getColor()))
.collect(groupingBy(Student::getColor));
打印Map結(jié)果:
{黑色=[Student{id=3, name='小牛', hobby='籃球', color='黑色', age=33}],
藍(lán)色=[Student{id=2, name='小白', hobby='足球', color='藍(lán)色', age=11}, Student{id=5, name='小綠', hobby='足球', color='藍(lán)色', age=24}]}
java9新增方法 filtering()方法,可以在分組時(shí)進(jìn)行過(guò)濾,以避免分組的結(jié)果map,因?yàn)檫^(guò)濾掉了一種key內(nèi)的所有元素導(dǎo)致map中沒(méi)有該key
filtering()
Map<String, List<Student>> collect = studentList.stream()
.collect(groupingBy(Student::getColor,
filtering(f->!"紅色".equals(f.getColor()),toList())));
打印Map結(jié)果:
{黑色=[Student{id=3, name='小牛', hobby='籃球', color='黑色', age=33}],
藍(lán)色=[Student{id=2, name='小白', hobby='足球', color='藍(lán)色', age=11}, Student{id=5, name='小綠', hobby='足球', color='藍(lán)色', age=24}],
紅色=[]}
4.在分組時(shí),將收集的對(duì)象映射處理為其他值
Map<String, List<String >> collect = studentList.stream()
.collect(groupingBy(Student::getColor,mapping(Student::getName,toList())));
打印Map結(jié)果:
{黑色=[小牛],
藍(lán)色=[小白, 小綠],
紅色=[小紅, 小羊]}
或?qū)apping()替換為其他方法對(duì)子組的數(shù)據(jù)進(jìn)行統(tǒng)計(jì)處理,比如maxBy()求子組中的Optional(最大值),或collectingAndThen(maxBy(),Optional::get)去除外層的Optional,直接獲得最大值.
或者替換為求和,sumInt等
5.在分組時(shí),如果收集的value為list中包list,想將兩層list轉(zhuǎn)為一層,可使用flatmapping()方法
java9新增方法
和java8的flatmap()方法一樣,通過(guò)將需要收集的集合元素A收集到總集合B時(shí),將A中的單個(gè)集合轉(zhuǎn)為Stream,和B的Stream通過(guò)flatmap()方法,將中間A的Stream去掉,當(dāng)A中的單個(gè)集合中的元素都拿出來(lái)放到B中.
兩層
Map<String,List<List<String>>> 將兩層List拆開(kāi) Map<String,List<String>>
Map<String, List<List<String >>> collect = studentList.stream()
.collect(groupingBy(Student::getHobby,
mapping(m->colorToMindMap.get(m.getColor()),toList())));
Map<String, List<String >> collect = studentList.stream()
.collect(groupingBy(Student::getHobby,
flatMapping(f->colorToMindMap.get(f.getColor()).stream(),toList())));
6.多級(jí)分組
所謂多級(jí)分組,就是在groupingBy(a,b)的第二個(gè)參數(shù)繼續(xù)放groupingBy(a,b)方法.規(guī)則和原來(lái)一樣.文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-455101.html
在平時(shí)用的groupingBy(Student::getColor)內(nèi)部調(diào)用的其實(shí)就是groupingBy(Student::getColor,toList()), toList()方法就代表著將元素按list的形式來(lái)存儲(chǔ)到對(duì)應(yīng)的key下,改成groupBy(a,b)就是以分組的方式繼續(xù)存儲(chǔ).文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-455101.html
到了這里,關(guān)于Java Stream流 Map 分組方式匯總的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!