【stream】使用 stream 流将多个 list 收集成一个 list,求和,排序,List 和 Map 互转,将 list 中数据根据条件进行分区

2 篇文章 0 订阅

目录

1、案例的前置准备

2、多个List 转为一个List

2.1、List 中的 List 

2.2、Map 中的 List

2.3、结果

3、 求和

3.1、BigDecimal

3.2、int 等

4、排序

4.1、正序

4.2、倒序

5、 List 与 Map 的互转

5.1、List 转 Map

5.2、Map 转 List

6、分区


1、案例的前置准备

实体:

2、多个List 转为一个List

flatmap

2.1、List 中的 List 

List<List<Student>> studentList = new ArrayList<>();
studentList.add(CollUtil.toList(new Student("张三", 12, new BigDecimal(2.3)), new Student("李四", 14, new BigDecimal(2.5))));
studentList.add(CollUtil.toList(new Student("王五", 22, new BigDecimal(5.6)), new Student("老六", 16, new BigDecimal(3.5))));

List<Student> students = studentList
	.stream()
    // flatMap(List::stream)把每个List转换成一个Stream,通过flatMap将它们连接成一个流
	.flatMap(List::stream)
    // 收集成 List
	.collect(Collectors.toList());
		
System.out.println(students);

 

2.2、Map 中的 List

 

Map<String, List<Student>> studentMap = new HashMap<>();
studentMap.put("第一个list", CollUtil.toList(new Student("张三", 12, new BigDecimal(2.3)), new Student("李四", 14, new BigDecimal(2.5))));
studentMap.put("第二个list", CollUtil.toList(new Student("王五", 22, new BigDecimal(5.6)), new Student("老六", 16, new BigDecimal(3.5))));

List<Student> mapList = studentMap
	.entrySet().stream()
    // 收集 map 中的 value
	.map(stringListEntry -> stringListEntry.getValue())
	.flatMap(List::stream)
	.collect(Collectors.toList());

System.out.println(mapList);

 

2.3、结果

2.1 和 2.2 的打印结果

[Test.Student(name=张三, age=12, money=2.29999999999999982236431605997495353221893310546875), Test.Student(name=李四, age=14, money=2.5), Test.Student(name=王五, age=22, money=5.5999999999999996447286321199499070644378662109375), Test.Student(name=老六, age=16, money=3.5)]

3、 求和

3.1、BigDecimal

List<List<Student>> studentList = new ArrayList<>();
studentList.add(CollUtil.toList(new Student("张三", 12, new BigDecimal(2.3)), new Student("李四", 14, new BigDecimal(2.5))));
studentList.add(CollUtil.toList(new Student("王五", 22, new BigDecimal(5.6)), new Student("老六", 16, new BigDecimal(3.5))));

BigDecimal totalMoney = studentList
	.stream()
	.flatMap(List::stream)
    // 收集 money 字段的值
	.map(Student::getMoney)
    // 没有初始值时第一个在就是初始值,BigDecimal.ZERO 相当于是 new BigDecimal(0)
    // 方法是 BigDecimal 的 add 方法
	.reduce(BigDecimal.ZERO, BigDecimal::add);

// 打印,保留两位小数向上取整
System.out.println(totalMoney.setScale(2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toString());

结果:

13.9

3.2、int 等

mapToXXX

List<List<Student>> studentList = new ArrayList<>();
studentList.add(CollUtil.toList(new Student("张三", 12, new BigDecimal(2.3)), new Student("李四", 14, new BigDecimal(2.5))));
studentList.add(CollUtil.toList(new Student("王五", 22, new BigDecimal(5.6)), new Student("老六", 16, new BigDecimal(3.5))));

int sumAge = studentList
	.stream()
	.flatMap(List::stream)
	.mapToInt(Student::getAge)
	.sum();

System.out.println(sumAge);

结果:

64

4、排序

sorted

4.1、正序

		List<List<Student>> studentList = new ArrayList<>();
		studentList.add(CollUtil.toList(new Student("张三", 12, new BigDecimal(2.3)), new Student("李四", 14, new BigDecimal(2.5))));
		studentList.add(CollUtil.toList(new Student("王五", 22, new BigDecimal(5.6)), new Student("老六", 16, new BigDecimal(3.5))));

		List<Student> students = studentList
			.stream()
			.flatMap(List::stream)
			.sorted(Comparator.comparing(Student::getAge))
			.collect(Collectors.toList());

		System.out.println(students);

结果:

[Test.Student(name=张三, age=12, money=2.29999999999999982236431605997495353221893310546875), Test.Student(name=李四, age=14, money=2.5), Test.Student(name=老六, age=16, money=3.5), Test.Student(name=王五, age=22, money=5.5999999999999996447286321199499070644378662109375)] 

4.2、倒序

 

		List<List<Student>> studentList = new ArrayList<>();
		studentList.add(CollUtil.toList(new Student("张三", 12, new BigDecimal(2.3)), new Student("李四", 14, new BigDecimal(2.5))));
		studentList.add(CollUtil.toList(new Student("王五", 22, new BigDecimal(5.6)), new Student("老六", 16, new BigDecimal(3.5))));

		List<Student> students = studentList
			.stream()
			.flatMap(List::stream)
			.sorted(Comparator.comparing(Student::getAge).reversed())
			.collect(Collectors.toList());

		System.out.println(students);

结果:

[Test.Student(name=王五, age=22, money=5.5999999999999996447286321199499070644378662109375), Test.Student(name=老六, age=16, money=3.5), Test.Student(name=李四, age=14, money=2.5), Test.Student(name=张三, age=12, money=2.29999999999999982236431605997495353221893310546875)]

5、 List 与 Map 的互转

5.1、List 转 Map

		List<List<Student>> studentList = new ArrayList<>();
		studentList.add(CollUtil.toList(new Student("张三", 12, new BigDecimal(2.3)), new Student("李四", 14, new BigDecimal(2.5))));
		studentList.add(CollUtil.toList(new Student("王五", 22, new BigDecimal(5.6)), new Student("老六", 16, new BigDecimal(3.5))));

		Map<String, List<Student>> collect = studentList
			.stream()
			.flatMap(List::stream)
			// 根据名称分组
			.collect(Collectors.groupingBy(Student::getName));
		System.out.println("将同名的收集在起来的Map:" + collect);

		Map<String, Student> studentMap = studentList
			.stream()
			.flatMap(List::stream)
			// 根据 名称 作为key 收集成一个Map,值就是当前对象,如果key1与key2重复,取key2的值
			.collect(Collectors.toMap(Student::getName, Function.identity(), (key1, key2) -> key2));
		System.out.println("根据名称收集成Map:" + studentMap);

结果:

将同名的收集在起来的Map:{李四=[Test.Student(name=李四, age=14, money=2.5)], 张三=[Test.Student(name=张三, age=12, money=2.29999999999999982236431605997495353221893310546875)], 老六=[Test.Student(name=老六, age=16, money=3.5)], 王五=[Test.Student(name=王五, age=22, money=5.5999999999999996447286321199499070644378662109375)]}


根据名称收集成Map:{李四=Test.Student(name=李四, age=14, money=2.5), 张三=Test.Student(name=张三, age=12, money=2.29999999999999982236431605997495353221893310546875), 老六=Test.Student(name=老六, age=16, money=3.5), 王五=Test.Student(name=王五, age=22, money=5.5999999999999996447286321199499070644378662109375)}

 

5.2、Map 转 List

		Map<String, Student> studentMap = new HashMap<>();
		studentMap.put("张三", new Student("张三", 12, new BigDecimal(2.3)));
		studentMap.put("王五", new Student("王五", 22, new BigDecimal(5.6)));
		studentMap.put("李四", new Student("李四", 14, new BigDecimal(2.5)));
		studentMap.put("老六", new Student("老六", 16, new BigDecimal(3.5)));

		List<Student> studentList = studentMap
			.entrySet()
			.stream()
			.map(studentEntry -> studentEntry.getValue())
			.collect(Collectors.toList());

		System.out.println(studentList);

结果:

[Test.Student(name=李四, age=14, money=2.5), Test.Student(name=张三, age=12, money=2.29999999999999982236431605997495353221893310546875), Test.Student(name=老六, age=16, money=3.5), Test.Student(name=王五, age=22, money=5.5999999999999996447286321199499070644378662109375)]

6、分区

		List<Student> studentList = new ArrayList<>();
		studentList.add(new Student("张三", 12, new BigDecimal(2.3)));
		studentList.add( new Student("王五", 22, new BigDecimal(5.6)));
		studentList.add( new Student("李四", 14, new BigDecimal(2.5)));
		studentList.add( new Student("老六", 16, new BigDecimal(3.5)));

		Map<Boolean, List<Student>> listMap = studentList
			.stream()
			// 根据年纪进行分区,将大于15岁的和小于15岁的分开存储
			.collect(Collectors.partitioningBy(student -> student.getAge() > 15));

		System.out.println(listMap);

结果:

{false=[Test.Student(name=张三, age=12, money=2.29999999999999982236431605997495353221893310546875), Test.Student(name=李四, age=14, money=2.5)],

true=[Test.Student(name=王五, age=22, money=5.5999999999999996447286321199499070644378662109375), Test.Student(name=老六, age=16, money=3.5)]}

这里只是举例的部分运用,stream 流还提供了很多方法。有兴趣可以多多学习使用。

 希望对你学习 java 的 stream 流有所帮助,多多支持!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值