Java List<Object>、List<Map<String,Object>>Stream流的相关用法

jdk1.8新增Stream流的相关用法梳理



一、List基于stream的操作

1、创建动态集合(of)

通过Stream.of()方法创建动态集合

List<StudentInfo> collect = Stream.of(new StudentInfo(1,"aa","男",18),
                new StudentInfo(2,"bb","女",19),new StudentInfo(3,"cc","女",20)).collect(Collectors.toList());

结果:
在这里插入图片描述

2、取出对象中的某一列(map)

通过集合的stream()方法取出对象中的某一列

List<String> collect1 = collect.stream().map(StudentInfo::getName).collect(Collectors.toList());

结果:
在这里插入图片描述

3、对指定条件的数据进行过滤(filter)

通过集合的stream()方法取出对象中的某一列

List<StudentInfo> collect2 = collect.stream().filter(stu -> stu.getAge() == 18).collect(Collectors.toList());

结果:
在这里插入图片描述

4、对指定数据进行分组(collect)

通过集合的stream()方法的collect方法,填入对应参数进行获取

Map<String, List<StudentInfo>> collect3 = collect.stream().collect(Collectors.groupingBy(StudentInfo::getSex, Collectors.toList()));

结果:
在这里插入图片描述

5、对指定字段进行求和(sum)

int sum = collect.stream().mapToInt(StudentInfo::getAge).sum(); // 57
double sum1 = collect.stream().mapToDouble(StudentInfo::getAge).sum(); // 57.0
long sum2 = collect.stream().mapToLong(StudentInfo::getAge).sum(); // 57

6、List和Map直接进行相互转换

(1)、将List集合转换为Map集合

Map<String, StudentInfo> collect4 = collect.stream().collect(Collectors.toMap(StudentInfo::getName, Function.identity()));

结果:
在这里插入图片描述

(2)、将Map集合转换为List集合

 Map<String,Object> map = new HashMap<>();
 map.put("id",1);
 map.put("name", "dd");
 map.put("age",23);

 List<Object> collect5 = new ArrayList<>(map.values());

结果:
在这里插入图片描述

7、进行判断操作

(1)、判断集合中是否有一条记录与之匹配

boolean b = collect.stream().anyMatch(stu -> stu.getAge() >= 20); // true

(2)、判断集合中是否所有的记录与之匹配

boolean b1 = collect.stream().allMatch(stu -> stu.getAge() >= 20); // false

8、计数

long count = collect.stream().filter(stu -> stu.getAge() >=19).count(); // 2

9、合并多个集合

(1)、第一种合并方法

List<StudentInfo> oneList = Stream.of(new StudentInfo(1,"aa","男",18),
                new StudentInfo(2,"bb","女",19),new StudentInfo(3,"cc","女",20)).collect(Collectors.toList());
List<StudentInfo> twoList = Stream.of(new StudentInfo(1,"aa","男",18),
     			new StudentInfo(2,"bb","女",19),new StudentInfo(3,"cc","女",20)).collect(Collectors.toList());
List<StudentInfo> threeList = Stream.of(new StudentInfo(1,"aa","男",18),
      			new StudentInfo(2,"bb","女",19),new StudentInfo(3,"cc","女",20)).collect(Collectors.toList());
// 合并操作      			
List<StudentInfo> collect6 = Stream.of(oneList, twoList, threeList).flatMap(Collection::stream).collect(Collectors.toList());

结果:
在这里插入图片描述

(2)、第二种合并方法

List<List<StudentInfo>> lists = Arrays.asList(oneList, twoList, threeList);
List<StudentInfo> collect7 = lists.stream().flatMap(Collection::stream).collect(Collectors.toList());

结果:
在这里插入图片描述

10、排序

(1)、升序排序

List<StudentInfo> sorted1 = collect.stream().sorted(Comparator.comparing(StudentInfo::getAge)).collect(Collectors.toList());// 正序

结果:
在这里插入图片描述

(2)、降序排序

List<StudentInfo> sorted2 = collect.stream().sorted(Comparator.comparing(StudentInfo::getAge).reversed()).collect(Collectors.toList()); // 降序

结果:在这里插入图片描述

二、List<Map<String,Object>>基于Stream的操作

测试数据:

Map<String,Object> one = new HashMap<>();
	one.put("id",1);
	one.put("name","aa");
	one.put("height",180.10);
Map<String,Object> two = new HashMap<>();
	two.put("id",2);
	two.put("name","bb");
	two.put("height",182.22);
Map<String,Object> three = new HashMap<>();
	three.put("id",3);
	three.put("name","cc");
	three.put("height",184.45);

1、创建动态集合

List<Map<String,Object>> list = Stream.of(one,two,three).collect(Collectors.toList());

结果:
在这里插入图片描述

2、取出对象中的某一列

List<Object> height = list.stream().map(map -> map.get("height")).collect(Collectors.toList());

结果:
在这里插入图片描述

3、对指定条件的数据进行过滤

        List<Map<String, Object>> height1 = list.stream().filter(map -> new BigDecimal(map.getOrDefault("height", 0).toString()).compareTo(BigDecimal.valueOf(182)) > 0).collect(Collectors.toList());

结果:
在这里插入图片描述

4、对指定数据进行分组

为方便测试此处加了一条数据

Map<String,Object> four = new HashMap<>();
	four.put("id",4);
	four.put("name","cc");
	four.put("height",186.66);
	list.add(four);
Map<Object, List<Map<String, Object>>> name = list.stream().collect(Collectors.groupingBy(map -> map.get("name")));

结果:
在这里插入图片描述

5、对指定字段进行求和

(1)、普通求和

double height2 = list.stream().mapToDouble(map -> Double.parseDouble(map.get("height").toString())).sum();

(2)、BIgDecimal进行求和

BigDecimal height3 = list.stream().map(map -> new BigDecimal(map.get("height").toString())).reduce(BigDecimal.ZERO, BigDecimal::add);

结果:
在这里插入图片描述

6、将List集合转换为Map

Map<Object, Map<String, Object>> id = list.stream().collect(Collectors.toMap(map -> map.get("id"), Function.identity()));

结果:
在这里插入图片描述

7、进行判断操作

(1)、判断是否有一条记录与之匹配

// true
boolean height4 = list.stream().anyMatch(map -> new BigDecimal(map.get("height").toString()).compareTo(BigDecimal.valueOf(182)) > 0);

(2)、判断是否所有记录与之匹配

// false
boolean height5 = list.stream().allMatch(map -> new BigDecimal(map.get("height").toString()).compareTo(BigDecimal.valueOf(182)) > 0);

8、计数

// 3
long height6 = list.stream().filter(map -> new BigDecimal(map.get("height").toString()).compareTo(BigDecimal.valueOf(182)) > 0).count();

9、排序

(1)、升序排序

List<Map<String, Object>> height7 = list.stream().sorted(Comparator.comparing(map -> new BigDecimal(map.get("height").toString()))).collect(Collectors.toList());

结果:
在这里插入图片描述

(2)、降序排序

List<Map<String, Object>> height8 = list.stream().sorted(Comparator.comparing((Map map) -> new BigDecimal(map.get("height").toString())).reversed()).collect(Collectors.toList());

结果:
在这里插入图片描述

  • 3
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LiJiangTao_DEV

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值