Java教程

java8 stream转map操作

本文主要是介绍java8 stream转map操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
  1 package com.example.mass_study.test01.anything;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 import java.util.Map;
  6 import java.util.concurrent.ConcurrentHashMap;
  7 import java.util.function.Function;
  8 import java.util.function.Predicate;
  9 import java.util.stream.Collectors;
 10 
 11 /**
 12  * @description:
 13  * @author: luguilin
 14  * @date: 2022-04-13 11:17
 15  **/
 16 
 17 
 18 public class StreamToMapTest {
 19     public static void main(String[] args) {
 20         List<User> users = new ArrayList<>();
 21         User user1 = new User(1, "1");
 22         User user2 = new User(2, "2");
 23         User user3 = new User(3, "3");
 24         users.add(user1);
 25         users.add(user2);
 26         users.add(user3);
 27         // 将id属性读取出来组成list
 28         List<Integer> idList = users.stream().map(User::getId).collect(Collectors.toList());
 29         System.out.println(idList);
 30 
 31         // 将id属性读取出来组成list 并去重
 32         List<Integer> idList2 = users.stream().map(User::getId).distinct().collect(Collectors.toList());
 33         System.out.println(idList2);
 34 
 35         // 组装成map
 36         Map<Integer, String> mp1 = users.stream().collect(Collectors.toMap(User::getId, User::getName));
 37         System.out.println(mp1);
 38 
 39         User user3_1 = new User(3, "3-1");
 40         users.add(user3_1);
 41 
 42         // 组装成map
 43         // key 重复会报错
 44         // Map<Integer, String> mp2 = users.stream().collect(Collectors.toMap(User::getId, User::getName));
 45         // System.out.println(mp1);
 46 
 47         // 组装成map 值取前面的
 48         Map<Integer, String> mp3 = users.stream().collect(Collectors.toMap(User::getId, User::getName, (v1, v2) -> v1));
 49         System.out.println(mp3);
 50 
 51         // 组装成map 值取后面的
 52         Map<Integer, String> mp2 = users.stream().collect(Collectors.toMap(User::getId, User::getName, (v1, v2) -> v2));
 53         System.out.println(mp2);
 54 
 55         // 组装成map 值取累加
 56         Map<Integer, String> mp4 = users.stream().collect(Collectors.toMap(User::getId, User::getName, (v1, v2) -> v1+v2));
 57         System.out.println(mp4);
 58 
 59         // 根据id 组装list对象,对应id的对象成组
 60         Map<Integer, List<User>> mp5 = users.stream().collect(Collectors.groupingBy(User::getId));
 61         System.out.println(mp5);
 62 
 63         // 根据id 组装list对象,对应id的对象的某个属性成组
 64         Map<Integer,List<String>> mp6 = users.stream().collect(Collectors.groupingBy(User::getId, Collectors.mapping(User::getName, Collectors.toList())));
 65         System.out.println(mp6);
 66 
 67         Map<Integer, List<String>> mp7 = users.stream().collect(Collectors.toMap(User::getId,
 68                 p ->  {
 69                     List<String> getNameList = new ArrayList<>();
 70                     getNameList.add(p.getName());
 71                     return getNameList;
 72                 },
 73                 (List<String> value1, List<String> value2) -> {
 74                     value1.addAll(value2);
 75                     return value1;
 76                 }
 77         ));
 78         System.out.println(mp7);
 79 
 80         User user4 = new User(4, null);
 81         users.add(user4);
 82 
 83         // 属性为空值,也可以输出
 84         Map<Integer, List<String>> mp8 = users.stream().collect(Collectors.toMap(User::getId,
 85                 p ->  {
 86                     List<String> getNameList = new ArrayList<>();
 87                     getNameList.add(p.getName());
 88                     return getNameList;
 89                 },
 90                 (List<String> value1, List<String> value2) -> {
 91                     value1.addAll(value2);
 92                     return value1;
 93                 }
 94         ));
 95         System.out.println(mp8);
 96 
 97         // 以对象为值
 98         Map<Integer, User> mp9 = users.stream()
 99                 .filter(distinctByKey(User::getId))
100                 .collect(Collectors.toMap(User::getId, User -> User));
101         System.out.println(mp9);
102     }
103 
104     public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
105         Map<Object,Boolean> seen = new ConcurrentHashMap<>();
106         return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
107     }
108 }
109 
110 class User{
111     private Integer id;
112     private String name;
113 
114     public User() {
115     }
116 
117     public User(Integer id, String name) {
118         this.id = id;
119         this.name = name;
120     }
121 
122     public Integer getId() {
123         return id;
124     }
125 
126     public void setId(Integer id) {
127         this.id = id;
128     }
129 
130     public String getName() {
131         return name;
132     }
133 
134     public void setName(String name) {
135         this.name = name;
136     }
137 }

 

这篇关于java8 stream转map操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!