JDK8系列一:Stream流
0、Account.java
1 package com.moon.bean; 2 3 import lombok.AllArgsConstructor; 4 import lombok.Data; 5 import lombok.NoArgsConstructor; 6 7 import java.io.Serializable; 8 9 @Data 10 @AllArgsConstructor 11 @NoArgsConstructor 12 public class Account implements Serializable { 13 /** 14 * 身份证号 15 */ 16 private String id; 17 18 /** 19 * 银行卡号 20 */ 21 private String bankCardNo; 22 23 /** 24 * 用户名 25 */ 26 private String username; 27 28 /** 29 * 手机号码 30 */ 31 private String mobile; 32 33 /** 34 * 邮箱 35 */ 36 private String email; 37 38 }
1、搜集list集合中的身份证号和用户名,将其转换成Map<String,String>
1 /** 2 * 搜集list集合中的身份证号和用户名,将其转换成Map<String,String> 3 */ 4 @Test 5 public void convertList2Map1() { 6 List<Account> accountList = new ArrayList<>(); 7 Account account1 = new Account("411856198009092331", "6220201", "刘备", "18513668898", "liubei@qq.com"); 8 Account account2 = new Account("411856198009092332", "6220202", "关羽", "18513668891", "guanyu@qq.com"); 9 Account account3 = new Account("411856198009092333", "6220203", "张飞", "18513668892", "zhangfei@qq.com"); 10 Account account4 = new Account("411856198009092334", "6220204", "赵云", "18513668893", "zhaoyun@qq.com"); 11 accountList.addAll(Arrays.asList(account1, account2, account3, account4)); 12 System.out.println(accountList.size()); 13 System.out.println(accountList); 14 for (Account account : accountList) { 15 System.out.println(account); 16 } 17 Map<String, String> streamMap = accountList.stream().collect(Collectors.toMap(Account::getId, Account::getUsername)); 18 System.out.println("streamMap.size():" + streamMap.size()); 19 System.out.println(JSON.toJSONString(streamMap)); 20 }

2、搜集list集合中的身份证号和账户实体,将其转换成Map<String,Account>
1 /** 2 * 搜集list集合中的身份证号和账户实体,将其转换成Map<String,Account> 3 * account -> account是一个返回本身lambda的表达式,其实还可以使用Function接口中的一个默认方法Function.identity(),这个方法返回自身对象,更加简洁 4 */ 5 @Test 6 public void convertList2Map2() { 7 List<Account> accountList = new ArrayList<>(); 8 Account account1 = new Account("411856198009092331", "6220201", "刘备", "18513668898", "liubei@qq.com"); 9 Account account2 = new Account("411856198009092332", "6220202", "关羽", "18513668891", "guanyu@qq.com"); 10 Account account3 = new Account("411856198009092333", "6220203", "张飞", "18513668892", "zhangfei@qq.com"); 11 Account account4 = new Account("411856198009092334", "6220204", "赵云", "18513668893", "zhaoyun@qq.com"); 12 accountList.addAll(Arrays.asList(account1, account2, account3, account4)); 13 Map<String, Account> streamMap1 = accountList.stream().collect(Collectors.toMap(Account::getId, account -> account)); 14 Map<String, Account> streamMap2 = accountList.stream().collect(Collectors.toMap(Account::getId, Function.identity())); 15 System.out.println("streamMap1.size():" + streamMap1.size()); 16 System.out.println("streamMap1 json:" + JSON.toJSONString(streamMap1)); 17 System.out.println("streamMap2.size():" + streamMap2.size()); 18 System.out.println("streamMap2 json:" + JSON.toJSONString(streamMap2)); 19 }

3、搜集list集合中的身份证号和账户实体,将其转换成Map<String,Account>,重复key情况
1 /** 2 * 搜集list集合中的身份证号和账户实体,将其转换成Map<String,Account>,重复key情况 3 * 在list转为map时,作为key的值有可能重复,这时候流的处理会抛出个异常:Java.lang.IllegalStateException:Duplicate key, 4 * 这时候就要在toMap方法中指定当key冲突时key的选择。(这里是选择第二个key2覆盖第一个key1) 5 */ 6 @Test 7 public void convertList2Map3() { 8 List<Account> accountList = new ArrayList<>(); 9 Account account1 = new Account("411856198009092331", "6220201", "刘备", "18513668898", "liubei@qq.com"); 10 Account account2 = new Account("411856198009092331", "6220202", "刘备", "18513668898", "liubei@qq.com"); 11 Account account3 = new Account("411856198009092333", "6220203", "张飞", "18513668892", "zhangfei@qq.com"); 12 Account account4 = new Account("411856198009092334", "6220204", "赵云", "18513668893", "zhaoyun@qq.com"); 13 accountList.addAll(Arrays.asList(account1, account2, account3, account4)); 14 //Map<String, Account> streamMap = accountList.stream().collect(Collectors.toMap(Account::getId, Function.identity())); 15 Map<String, Account> streamMap = accountList.stream().collect(Collectors.toMap(Account::getId, Function.identity(),(key1,key2) -> key2)); 16 System.out.println(streamMap.size()); 17 System.out.println(JSON.toJSONString(streamMap)); 18 }

参考:https://blog.csdn.net/zlj1217/article/details/81611834

浙公网安备 33010602011771号