Java List<Object>、List<Map<String,Object>>Stream流的相关用法
通过集合的stream()方法的collect方法,填入对应参数进行获取。通过集合的stream()方法取出对象中的某一列。通过集合的stream()方法取出对象中的某一列。通过Stream.of()方法创建动态集合。为方便测试此处加了一条数据。
·
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());
结果:
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献1条内容
所有评论(0)