大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
groupingBy用于分组,toMap用于list转map
测试代码:
1.建一个实体类,测试中用
package com.xhx.java;
/**
* xuhaixing
* 2018/7/20 21:43
**/
public class Student {
private String name;
private String sex;
private double money;
public Student(String name, String sex, double money) {
this.name = name;
this.sex = sex;
this.money = money;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", money=" + money +
'}';
}
}
2.具体测试代码及输出结果
package com.xhx.java;
import org.junit.Test;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
/**
* xuhaixing
* 2018/7/20 21:44
**/
public class TestCollectors {
/**
* Collectors.groupingBy 分组1
*/
@Test
public void testGrouping() {
List<String> items = Arrays.asList("apple", "banana", "apple", "orange", "banana");
Collector<Object, ?, Map<Object, Long>> objectMapCollector = Collectors.groupingBy(Function.identity(), Collectors.counting());
Map<Object, Long> collect = items.stream().collect(objectMapCollector);
System.out.println(collect);
//{orange=1, banana=2, apple=2}
}
/**
* Collectors.groupingBy 分组2
*/
@Test
public void testGrouping2() {
List<Student> students = Arrays.asList(new Student("apple", "男", 10.0),
new Student("banana", "男", 10.0),
new Student("orange", "男", 20.0),
new Student("pipe", "女", 40.0),
new Student("pinck", "女", 80.0)
);
//根据sex分组
Map<String, List<Student>> collect = students.stream().collect(Collectors.groupingBy(Student::getSex));
System.out.println(collect);
//{
// 女=[Student{name='pipe', sex='女', money=40.0}, Student{name='pinck', sex='女', money=80.0}],
// 男=[Student{name='apple', sex='男', money=10.0}, Student{name='banana', sex='男', money=10.0}, Student{name='orange', sex='男', money=20.0}]
// }
//根据sex分组,然后对money求和
Map<String, Double> collect1 = students.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.summingDouble(Student::getMoney)));
System.out.println(collect1);
//{女=120.0, 男=40.0}
}
/**
* list、set
*/
@Test
public void testGrouping3() {
List<Student> students = Arrays.asList(new Student("apple", "男", 10.0),
new Student("banana", "男", 10.0),
new Student("orange", "男", 20.0),
new Student("pipe", "女", 40.0),
new Student("pinck", "女", 80.0)
);
Map<String, List<Student>> collect1 = students.stream().collect(Collectors.groupingBy(Student::getSex));
System.out.println(collect1);
//{
// 女=[Student{name='pipe', sex='女', money=40.0}, Student{name='pinck', sex='女', money=80.0}],
// 男=[Student{name='apple', sex='男', money=10.0}, Student{name='banana', sex='男', money=10.0}, Student{name='orange', sex='男', money=20.0}]
// }
Map<String, List<Double>> collect2 = students.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.mapping(Student::getMoney, Collectors.toList())));
System.out.println(collect2);
//{女=[40.0, 80.0], 男=[10.0, 10.0, 20.0]}
Map<String, Set<Double>> collect3 = students.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.mapping(Student::getMoney, Collectors.toSet())));
System.out.println(collect3);
//{女=[40.0, 80.0], 男=[10.0, 20.0]}
}
/**
* toMap 转成map
*/
@Test
public void tesToMap(){
List<Student> students = Arrays.asList(new Student("apple", "男", 10.0),
new Student("banana", "男", 10.0),
new Student("orange", "男", 20.0),
new Student("pipe", "女", 40.0),
new Student("pinck", "女", 80.0)
);
Map<String, Double> collect = students.stream().collect(Collectors.toMap(Student::getName, Student::getMoney));
System.out.println(collect);
//{orange=20.0, banana=10.0, apple=10.0, pinck=80.0, pipe=40.0}
}
/**
* toMap 转成map key重复
*/
@Test
public void tesToMap2(){
List<Student> students = Arrays.asList(new Student("apple", "男", 10.0),
new Student("banana", "男", 10.0),
new Student("orange", "男", 20.0),
new Student("pipe", "女", 40.0),
new Student("pinck", "女", 80.0)
);
/*
java.lang.IllegalStateException: Duplicate key apple
at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1254)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
*/
// Map<Double,String> collect2 = students.stream().collect(Collectors.toMap(Student::getMoney, Student::getName));
// System.out.println(collect2);
Map<Double,String> collect2 = students.stream().collect(Collectors.toMap(Student::getMoney, Student::getName,(oldValue,newValue)->newValue));
System.out.println(collect2);
//{80.0=pinck, 40.0=pipe, 20.0=orange, 10.0=banana} key重复用新值
}
/**
* toMap 转成map 排序
*/
@Test
public void tesToMap3() {
List<Student> students = Arrays.asList(new Student("apple", "男", 10.0),
new Student("banana", "男", 10.0),
new Student("orange", "男", 20.0),
new Student("pipe", "女", 40.0),
new Student("pinck", "女", 80.0)
);
LinkedHashMap<String, Double> collect = students.stream().sorted(Comparator.comparing(Student::getMoney).reversed())
.collect(Collectors.toMap(Student::getName, Student::getMoney, (oldValue, newValue) -> newValue, LinkedHashMap::new));
System.out.println(collect);
//{pinck=80.0, pipe=40.0, orange=20.0, apple=10.0, banana=10.0}
}
}
需要注意list转set,set中的值不允许有重复,所以只有一个,,list转map,map的key不能重复,所以自己设置选用老的还是新的,还一点需要注意,list转map时key可以为null,value不可以为null,否则会报空指针异常
实时内容请关注微信公众号,公众号与博客同时更新:程序员星星
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/171472.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...