目录
1. list转化map基本操作
id最常用方式:
-> 01 key-value值形式:
-> 02 id-> 对象本身
-> 03 id-> 对象本身的 lambda写法
->04 解决冲突的key
2. list计算操作
3. list转化泛型操作
4. List转成map的进阶操作
->4.1 有实体类的进阶操作(常用)
--->准备工作01 : 基础数据(User实体类)
--->4.1.1: (1对1关系) 分组: 用户名-> 用户实体类对象User(String->T)
--->4.1.2 (1对1关系) 用户名 -> 电话号(String-> String)
--->4.1.301: (1对多) 分组01: 根据年龄分组 Integer-> List
--->4.1.302: (1对多) 分组01: 根据年龄分组 Integer-> List 方法二
--->4.1.4: (1对多) 分组 根据年龄分出 年龄-> 用户 Integer -> List
-> 4.2 没有实体类的进阶操作
[待续未完....时间有限]
-> 5. 总结: 4整个的main方法
-> 5..1 不啰嗦 直接上代码
1. list转化map基本操作
id最常用方式:
-> 01 key-value值形式:
Map<Integer,String>map = list.stream().collect(Collectors.toMap(User::getId,User::getRealName))
-> 02 id-> 对象本身
Map<Integer,User> userMap3 = list.stream().collect(Collectors.toMap(User::getId, Function.identity()));
-> 03 id-> 对象本身的 lambda写法
Map<Integer,User> userMap2 = userList.stream().collect(Collectors.toMap(User::getId,User->User));
->04 解决冲突的key
Map<Integer,User> userMap4 = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(),(key1,key2)->key2));
2. list计算操作
List<Long> testList = new ArrayList<>(Collections.nCopies(5, 0L));
testList.set(0,1L);
testList.set(1,2L);
testList.set(2,3L);
testList.set(3,4L);
testList.set(4,5L);
System.out.println("sum1 is " + testList.stream().reduce(0L, (a, b) -> a + b));
// reduce根据初始值(参数1)和累积函数(参数2)依次对数据流进行操作,第一个值与初始值送入累积函数,后面计算结果和下一个数据流依次送入累积函数。
System.out.println("sum2 is " + testList.stream().reduce(0L, Long::sum));
System.out.println("sum3 is " + testList.stream().collect(Collectors.summingLong(Long::longValue)));
// Collectors.summingLong()将流中所有元素视为Long类型,并计算所有元素的总和
System.out.println("sum4 is " + testList.stream().mapToLong(Long::longValue).sum());
System.out.println("***********************");
List<Person> testList1 = new ArrayList<>(Collections.nCopies(5, new Person(1)));
System.out.println("class sum1 is " + testList1.stream().map(e -> e.getAge()).reduce(0, (a,b) -> a + b));
System.out.println("class sum2 is " + testList1.stream().map(e -> e.getAge()).reduce(0, Integer::sum));
System.out.println("class sum3 is " + testList1.stream().collect(Collectors.summingInt(Person::getAge)));
System.out.println("class sum4 is " + testList1.stream().map(e -> e.getAge()).mapToInt(Integer::intValue).sum());
3. list转化泛型操作
List<User> list = userMapper.selectUserMessage(null);
List<UserRespDTO> collect = list.stream().map(dto-> {
UserRespDTO userRespDTO = new UserRespDTO();
BeanUtils.copyProperties(dto, respDTO);
return userRespDTO;
}).collect(Collectors.toList());
4. List转成map的进阶操作
->4.1 有实体类的进阶操作(常用)
--->准备工作01 : 基础数据(User实体类)
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.util.Date;
/**
* 模拟用户实体类对象
*
* @author pzy
* @version 0.1.0
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class User {
private String username;
private Integer age;
private String telephone;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
}
---> 准备工作02:
//1. 创建基础数据
List<User> list = new ArrayList<>();
User user1 = new User("张三", 33, "13345678913", new Date());
User user2 = new User("李四", 44, "13345678914", new Date());
User user3 = new User("王五", 55, "13345678915", new Date());
User user4 = new User("李六", 55, "13345678916", new Date());
list.add(user1);
list.add(user2);
list.add(user3);
list.add(user4);
--->4.1.1: (1对1关系) 分组: 用户名-> 用户实体类对象User(String->T)
Map<String, User> map1 = list.stream().collect(Collectors.toMap(User::getUsername, each -> each, (value1, value2) -> value1));
System.out.println(JSON.toJSONString(map1));
//{"李四":{"age":44,"createTime":1679536527360,"telephone":"13345678914","username":"李四"},"张三":{"age":33,"createTime":1679536527360,"telephone":"13345678913","username":"张三"},"李六":{"age":55,"createTime":1679536527360,"telephone":"13345678916","username":"李六"},"王五":{"age":55,"createTime":1679536527360,"telephone":"13345678915","username":"王五"}}
--->4.1.2 (1对1关系) 用户名 -> 电话号(String-> String)
Map<String,String> map2 = list.stream().collect(Collectors.toMap(User::getUsername,User::getTelephone,(value1, value2) -> value1));
System.out.println(JSON.toJSONString(map2));
//{"李四":"13345678914","张三":"13345678913","李六":"13345678916","王五":"13345678915"}
--->4.1.301: (1对多) 分组01: 根据年龄分组 Integer-> List<T>
Map<Integer, List<User>> map301 = list.stream().collect(Collectors.groupingBy(User::getAge));
System.out.println(JSON.toJSONString(map301));
//{33:[{"age":33,"createTime":1679536527360,"telephone":"13345678913","username":"张三"}],55:[{"age":55,"createTime":1679536527360,"telephone":"13345678915","username":"王五"},{"age":55,"createTime":1679536527360,"telephone":"13345678916","username":"李六"}],44:[{"age":44,"createTime":1679536527360,"telephone":"13345678914","username":"李四"}]}
--->4.1.302: (1对多) 分组01: 根据年龄分组 Integer-> List<T> 方法二
Map<Integer,List<User>> map302 = list.stream().collect(Collectors.toMap(User::getAge, Collections::singletonList,(value1, value2) -> {
List<User> union = new ArrayList<>(value1);
union.addAll(value2);
return union;
}));
System.out.println(JSON.toJSONString(map302));
//{33:[{"age":33,"createTime":1679536527360,"telephone":"13345678913","username":"张三"}],55:[{"age":55,"createTime":1679536527360,"telephone":"13345678915","username":"王五"},{"age":55,"createTime":1679536527360,"telephone":"13345678916","username":"李六"}],44:[{"age":44,"createTime":1679536527360,"telephone":"13345678914","username":"李四"}]}
--->4.1.4: (1对多) 分组 根据年龄分出 年龄-> 用户 Integer -> List<String>
业务: 想查看每个年龄下都有谁(姓名即可)
Map<Integer,List<String>> map4 = list.stream().collect(Collectors.toMap(User::getAge,each->Collections.singletonList(each.getUsername()),(value1, value2) -> {
List<String> usernameList = new ArrayList<>(value1);
usernameList.addAll(value2);
return usernameList;
}));
System.out.println(JSON.toJSONString(map4));
//{33:["张三"],55:["王五","李六"],44:["李四"]}
-> 4.2 没有实体类的进阶操作
[待续未完....时间有限]
-> 5. 总结: 4整个的main方法
-> 5..1 不啰嗦 直接上代码
public static void main(String[] args) {
//1. 创建基础数据
List<User> list = new ArrayList<>();
User user1 = new User("张三", 33, "13345678913", new Date());
User user2 = new User("李四", 44, "13345678914", new Date());
User user3 = new User("王五", 55, "13345678915", new Date());
User user4 = new User("李六", 55, "13345678916", new Date());
list.add(user1);
list.add(user2);
list.add(user3);
list.add(user4);
/*1. (1对1关系) 分组: 用户名-> 用户实体类对象User*/
Map<String, User> map1 = list.stream().collect(Collectors.toMap(User::getUsername, each -> each, (value1, value2) -> value1));
System.out.println(JSON.toJSONString(map1));
//{"李四":{"age":44,"createTime":1679536527360,"telephone":"13345678914","username":"李四"},"张三":{"age":33,"createTime":1679536527360,"telephone":"13345678913","username":"张三"},"李六":{"age":55,"createTime":1679536527360,"telephone":"13345678916","username":"李六"},"王五":{"age":55,"createTime":1679536527360,"telephone":"13345678915","username":"王五"}}
/*2. (1对1关系) 用户名 -> 电话号 */
Map<String,String> map2 = list.stream().collect(Collectors.toMap(User::getUsername,User::getTelephone,(value1, value2) -> value1));
System.out.println(JSON.toJSONString(map2));
//{"李四":"13345678914","张三":"13345678913","李六":"13345678916","王五":"13345678915"}
/*301. (1对多关系) 分组01: 根据年龄分组 目的: 将年龄相等的进行分组 年龄->用户实体类 */
Map<Integer, List<User>> map301 = list.stream().collect(Collectors.groupingBy(User::getAge));
System.out.println(JSON.toJSONString(map301));
//{33:[{"age":33,"createTime":1679536527360,"telephone":"13345678913","username":"张三"}],55:[{"age":55,"createTime":1679536527360,"telephone":"13345678915","username":"王五"},{"age":55,"createTime":1679536527360,"telephone":"13345678916","username":"李六"}],44:[{"age":44,"createTime":1679536527360,"telephone":"13345678914","username":"李四"}]}
/*302. (1对多关系) 分组02: 根据年龄分组 目的: 将年龄相等的进行分组 年龄->用户实体类 */
Map<Integer,List<User>> map302 = list.stream().collect(Collectors.toMap(User::getAge, Collections::singletonList,(value1, value2) -> {
List<User> union = new ArrayList<>(value1);
union.addAll(value2);
return union;
}));
System.out.println(JSON.toJSONString(map302));
//{33:[{"age":33,"createTime":1679536527360,"telephone":"13345678913","username":"张三"}],55:[{"age":55,"createTime":1679536527360,"telephone":"13345678915","username":"王五"},{"age":55,"createTime":1679536527360,"telephone":"13345678916","username":"李六"}],44:[{"age":44,"createTime":1679536527360,"telephone":"13345678914","username":"李四"}]}
/*4. (1对多关系) 分组 根据年龄分出 年龄-> 用户 业务: 想查看每个年龄下都有谁(姓名即可)*/
Map<Integer,List<String>> map4 = list.stream().collect(Collectors.toMap(User::getAge,each->Collections.singletonList(each.getUsername()),(value1, value2) -> {
List<String> usernameList = new ArrayList<>(value1);
usernameList.addAll(value2);
return usernameList;
}));
System.out.println(JSON.toJSONString(map4));
//{33:["张三"],55:["王五","李六"],44:["李四"]}
}
创作不易, 点个赞再走呗~~~
近期评论