Java8 将一个List<T>转为Map<String, T>

JAVA8 新特性 专栏收录该内容
20 篇文章 0 订阅

将 List 转为 Map<String, T>

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);
    }
}

程序运行输出

{Answer=1, AnswerAIL=1, AI=1}

 

将 List 转为 Map<K, V>

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);
}

程序运行输出

[{"age":78,"name":"answer0"},{"age":89,"name":"answer1"},{"age":72,"name":"answer2"}]

{answer2=72, answer1=89, answer0=78}

 

将 List 转为 Map<String, T>

实现方式1

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

程序运行输出

[{"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

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));
    }
}    

程序运行输出

[{"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 实体

@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;
    }
}
  • 1
    点赞
  • 2
    评论
  • 7
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值