将 List 转为 Map<String, T>
1
2
3
4
5
6
7
|
public class AnswerApp { public static void main(String[] args) throws Exception { List<String> names = Lists.newArrayList( "Answer" , "AnswerAIL" , "AI" ); Map<String, Integer> map = names.stream().collect(Collectors.toMap(v -> v, v -> 1 )); System.out.println(map); } } |
程序运行输出
1
|
{Answer= 1 , AnswerAIL= 1 , AI= 1 } |
将 List 转为 Map<K, V>
1
2
3
4
5
6
7
8
9
10
|
public static void main(String[] args) throws Exception { List<User> users = new ArrayList<>(); for ( int i = 0 ; i < 3 ; i++) { users.add( new User( "answer" + i, new Random().nextInt( 100 ))); } System.out.println(JSON.toJSONString(users)); System.out.println(); Map<String, Integer> map = users.stream().collect(Collectors.toMap(User::getName, User::getAge)); System.out.println(map); } |
程序运行输出
1
2
|
[{ "age" : 78 , "name" : "answer0" },{ "age" : 89 , "name" : "answer1" },{ "age" : 72 , "name" : "answer2" }] {answer2= 72 , answer1= 89 , answer0= 78 } |
将 List 转为 Map<String, T>
实现方式1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class AnswerApp { public static void main(String[] args) throws Exception { List<User> users = new ArrayList<>(); for ( int i = 0 ; i < 3 ; i++) { // 改为此代码, 转map时会报错 Duplicate key User // users.add(new User("answer", new Random().nextInt(100))); users.add( new User( "answer" + i, new Random().nextInt( 100 ))); } System.out.println(JSON.toJSONString(users)); System.out.println(); Map<String, User> map = users.stream().collect(Collectors.toMap(User::getName, Function.identity())); System.out.println(JSON.toJSONString(map)); } } |
该方式如果 map 的 key(如上述例子的 User::getName 的值) 重复, 会抛错java.lang.IllegalStateException: Duplicate key User
程序运行输出
1
2
|
[{ "age" : 22 , "name" : "answer0" },{ "age" : 79 , "name" : "answer1" },{ "age" : 81 , "name" : "answer2" }] { "answer2" :{ "age" : 81 , "name" : "answer2" }, "answer1" :{ "age" : 79 , "name" : "answer1" }, "answer0" :{ "age" : 22 , "name" : "answer0" }} |
实现方式2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class AnswerApp { public static void main(String[] args) throws Exception { List<User> users = new ArrayList<>(); for ( int i = 0 ; i < 3 ; i++) { users.add( new User( "answer" , new Random().nextInt( 100 ))); } System.out.println(JSON.toJSONString(users)); System.out.println(); // 如果 key 重复, 则根据 冲突方法 ·(key1, key2) -> key2· 判断. 解释: key1 key2 冲突时 取 key2 Map<String, User> map = users.stream().collect(Collectors.toMap(User::getName, Function.identity(), (key1, key2) -> key2)); System.out.println(JSON.toJSONString(map)); } } |
程序运行输出
1
2
|
[{ "age" : 24 , "name" : "answer" },{ "age" : 89 , "name" : "answer" },{ "age" : 68 , "name" : "answer" }] { "answer" :{ "age" : 68 , "name" : "answer" }} |
如果改为 (key1, key2) -> key1 则输出 {"answer":{"age":24,"name":"answer"}}
User 实体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@Data @NoArgsConstructor @AllArgsConstructor public class User { private Long id; private String name; private Integer age; public User(String name) { this .name = name; } public User(String name, Integer age) { this .name = name; this .age = age; } } |
补充:java8中使用Lambda表达式将list中实体类的两个字段转Map
代码:
1
2
|
List<Entity> list = new ArrayList<>(); Map<Integer, String> map = list.stream().collect(Collectors.toMap(Entity::getId, Entity::getType)); |
常用的lambda表达式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
** * List -> Map * 需要注意的是: * toMap 如果集合对象有重复的key,会报错Duplicate key .... * apple1,apple12的id都为 1 。 * 可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2 */ Map<Integer, Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1)); 安照某一字段去重 list = list.stream().filter(distinctByKey(p -> ((ModCreditColumn) p).getFieldCode())).collect(Collectors.toList()); List<Double> unitNetValue = listIncreaseDto.stream().map(IncreaseDto :: getUnitNetValue).collect(Collectors.toList()); //求和 对象List BigDecimal allFullMarketPrice = entityList.stream().filter(value -> value.getFullMarketPrice()!= null ).map(SceneAnalysisRespVo::getFullMarketPrice).reduce(BigDecimal.ZERO, BigDecimal::add); List<BigDecimal> naturalDayList; BigDecimal total = naturalDayList.stream().reduce(BigDecimal.ZERO, BigDecimal::add); 分组函数 Map<String, List<SceneAnalysisRespVo>> groupMap = total.getGroupList().stream().collect(Collectors.groupingBy(SceneAnalysisRespVo::getVmName)); //DV01之和 BigDecimal allDV01 = values.stream().filter(sceneAnalysisRespVo -> sceneAnalysisRespVo.getDv() != null ).map(SceneAnalysisRespVo::getDv).reduce(BigDecimal.ZERO, BigDecimal::add); |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://jaemon.blog.csdn.net/article/details/92685846