Java Stream流处理Map 详细用法
实体类
package com.jzj.model;
/**
* 当前描述:
*
* @author: jiazijie
* @since: 2021/7/23 下午4:06
*/
public class Person {
private Integer id;
private String name;
private Integer age;
private String grade;
public Person(Integer id, String name, Integer age, String grade) {
this.id = id;
this.name = name;
this.age = age;
this.grade = grade;
}
public String getName() {
return name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
Stream方法
package com.jzj.util;
import com.alibaba.fastjson.JSONObject;
import com.xrxs.jzj.model.Person;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.toSet;
/**
* 当前描述:
*
* @author: jiazijie
* @since: 2021/7/23 下午3:26
*/
public class Test {
public static void main(String[] args) {
List list = new ArrayList<>();
Person p1 = new Person(1, "a", 18, "A");
Person p2 = new Person(2, "b", 19, "B");
Person p3 = new Person(3, "c", 18, "C");
Person p4 = new Person(4, "a", 21, "A");
Person p5 = new Person(5, "a", 18, "B");
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
/**
* 获取 每个ID对应的 Person Map
*/
Map mapObj = list.stream().collect(Collectors.toMap(Person::getId, Function.identity(), (v1, v2) -> v1));
// {1:{"age":18,"grade":"A","id":1,"name":"a"},2:{"age":19,"grade":"B","id":2,"name":"b"},3:{"age":18,"grade":"C","id":3,"name":"c"},4:{"age":21,"grade":"A","id":4,"name":"a"},5:{"age":18,"grade":"B","id":5,"name":"a"}}
System.out.println(JSONObject.toJSONString(mapObj));
// 获取 每个名字 对应的ID, 如果有重复的名字,就会报错!!!!!Duplicate key 1 比如 a-1,a-4,a-5 这三个同样的名字对应不同的id
// Map mapNameId =list.stream().collect(Collectors.toMap(Person::getName,Person::getId));
// System.out.println(JSONObject.toJSONString(mapNameId));
/**
* 为了避免这种情况,要制定 value (v1,v2)->v1 用第一次出现的value还是最后一次出现的value v1 用第一次出现的
* v2用最后一次出现的
*/
Map mapNameId1 = list.stream().collect(Collectors.toMap(Person::getName, Person::getId, (v1, v2) -> v1));
Map mapNameId2 = list.stream().collect(Collectors.toMap(Person::getName, Person::getId, (v1, v2) -> v2));
// {"a":1,"b":2,"c":3}
System.out.println(JSONObject.toJSONString(mapNameId1));
// {"a":5,"b":2,"c":3}
System.out.println(JSONObject.toJSONString(mapNameId2));
/**
* 获取 每个班级的 人员集合,以 班级为分组 group
*/
Map> gradePersonsMap = list.stream().collect(Collectors.groupingBy(Person::getGrade));
// {"A":[{"age":18,"grade":"A","id":1,"name":"a"},{"age":21,"grade":"A","id":4,"name":"a"}],"B":[{"age":19,"grade":"B","id":2,"name":"b"},{"age":18,"grade":"B","id":5,"name":"a"}],"C":[{"age":18,"grade":"C","id":3,"name":"c"}]}
System.out.println(JSONObject.toJSONString(gradePersonsMap));
/**
* 获取 每个班级的 人员年龄的Set集合 以班级为分组,年龄去重
*/
Map> gradeAgesMap = list.stream()
.collect(Collectors.groupingBy(Person::getGrade, Collectors.mapping(Person::getAge, Collectors.toSet())));
// {"A":[18,21],"B":[18,19],"C":[18]}
System.out.println(JSONObject.toJSONString(gradeAgesMap));
/**
* 获取 每个班级的 人员ID的List集合 以班级为分组,人员ID
*/
Map> gradePIdListMap = list.stream()
.collect(Collectors.groupingBy(Person::getGrade, Collectors.mapping(Person::getId, Collectors.toList())));
// {"A":[1,4],"B":[2,5],"C":[3]}
System.out.println(JSONObject.toJSONString(gradePIdListMap));
/**
* 获取 每个班级的 人员集合,以 班级为分组 group
*/
Map> gradePersons = list.stream().collect(Collectors.groupingBy(Person::getGrade));
以Map开始遍历 获取班级 中年龄的 去重集合
Map> gradeAgeSetMap = gradePersons.entrySet().stream().collect(
Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().stream().map(Person::getAge).collect(toSet()), (v1, v2) -> v1));
// {"A":[18,21],"B":[18,19],"C":[18]}
System.out.println(JsonUtil.toJson(gradeAgeSetMap));
}
}