目录
java.util.stream.Stream#flatMap
java.util.stream.Stream
list => map
Collectors.toMap,Collectors.groupingBy
public class TestData {
private int id;
private String name;
private String sex;
private String subject;
private String score;
}
public class TestDataKey {
private String name;
private String sex;
}
1、获取每个人所学的科目列表:List<TestData> => Map<Integer, List<String>>
Map<String, List<String>> idAndSujectListMap= list.stream().collect(Collectors.groupingBy(TestData::getId,
Collectors.mapping(TestData::getSubject, Collectors.toList())));
//去重
Map<String, Set<String>> idAndSujectListMap= list.stream().collect(Collectors.groupingBy(TestData::getId,
Collectors.mapping(TestData::getSubject, Collectors.toSet())));
2、获取每个人的总成绩:List<TestData> => Map<Integer, Integer>
Map<Integer, Integer> idAndScoreSumMap= list.stream().collect(Collectors.groupingBy(TestData::getId,
Collectors.summingInt(TestData::getScore));
3、获取name+sex(唯一键)对应的所有信息:List<TestData> => Map<TestDataKey, List<TestData>>
Map<TestDataKey, List<TestData>> keyAndListMap = list.stream().collect(Collectors.groupingBy(item ->
TestDataKey.builder().name(item.getName()).sex(item.getSex()).build()));
4、获取name+sex(唯一键)对应总成绩:List<TestData> => Map<TestDataKey, Integer>
Map<TestDataKey, Integer> keyAndScoreSumMap = list.stream().collect(Collectors.groupingBy(item ->
TestDataKey.builder().name(item.getName()).sex(item.getSex()).build(),
Collectors.summingInt(TestData::getScore)));
5、获取name+sex(唯一键)对应的ID:List<TestData> => Map<TestDataKey, Integer>
Map<TestDataKey, Integer> keyAndIdMap = list.stream().collect(Collectors.toMap(item ->
TestDataKey.builder().name(item.getName()).sex(item.getSex()).build(),
TestData::getId, (v1, v2) -> v1));
map => map 数据处理
Map<Integer, String> 转 Map<Integer, List<String>>
将map的value(逗号隔开的字符串)转为List<String>
Map<Integer, List<String>> resultMap = map.entrySet()
.stream().collect(Collectors.toMap(Map.Entry::getKey,
entry -> Arrays.asList(entry.getValue().split(","))));
java.util.stream.Stream#flatMap
使用场景:map不适用时,有多个元素时,考虑使用
1、获取所有学科:Map<Integer, List<String>> idAndSubjectMap => List<String> allSubject
//已有:Map<Integer, List<String>> idAndSubjectMap
//之前
List<Integer> subjects = Lists.newArrayList();
idAndSubjectMap.values().forEach(subjects::addAll);
//替换
List<Integer> subjects = idAndSubjectMap.values().stream().flatMap(Collection::stream).collect(Collectors.toList())
2、获取所有学科:Map<Integer, String> idAndSubjectMap => List<String> allSubject
假设map的value学科是以逗号隔开的字符串
//已有:Map<Integer, String> idAndSubjectMap
//之前
List<String> subjects = Lists.newArrayList();
idAndSubjectMap.values().forEach(item -> subjects.addAll(Arrays.asList(item.split(","))));
//替换
List<String> subjects = idAndSubjectMap.values().stream()
.flatMap(value-> Arrays.stream(value.split(","))).collect(Collectors.toList());
3、获取双层map的内部map的keylist
Map<String, Map<Integer, Integer>> factoryMihomeNumMap;
List<Integer> mihomeIdList = factoryMihomeNumMap.values().stream().map(Map::keySet).flatMap(Set::stream).distinct().collect(Collectors.toList());