从list集合中取某一个属性值操作

List<String> idList=list.stream().map(Order::getId()).collect(Collectors.toList());
System.out.println(idList)
结果

输出第一个:

["MCS-2019-1123", "MCS-2019-1124", "MCS-2019-1125"]

[1, 2, 3]

1、遍历-foreach

list.stream().forEach(student->{
  //处理逻辑,打印出所有学生的姓名
  System.out.println(student.getName());
});

2、筛选list

filter函数的()里,应该放逻辑,判断条件,将符合条件的放到resultList中

代码如下,筛选集合中所有性别为女的学生

List<Student> resultList = list.stream().filter(student -> Objects.equals(student.getSex(),"女")).collect(Collectors.toList());
resultList.stream().forEach(student->{
  System.out.println(student.getName());
});

3、list去重

根据性别去重

List<Student> unique = list.stream().collect(Collectors.collectingAndThen(
   Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getSex))), ArrayList::new)); 
unique.stream().forEach(student->{
  System.out.println(student.getName());
});

4、取出list集合对象中某一个属性

取出每个对象中的姓名组成一个新的集合

List<String> listStr = list.stream().map(Student::getName).collect(Collectors.toList());

去重

List<String> listNew = listStr .stream().map(Student::getName).distinct().collect(Collectors.toList());

5、list与map互转,并根据某一属性进行分组

list转map (下方studentMap运行会报错,因为作为key值,name不能重复,所以正式开发中应该使用唯一性id作为key值)

Map<String, Student> studentMap = list.stream().collect(Collectors.toMap(Student::getName, student -> student));

list转数组

String[] listStrs = list.stream()
        .filter(e -> Objects.equals(e.getSex(), "男"))
        .sorted(Comparator.comparing(Student::getName))
        .map(Student::getName).toArray(String[]::new);
list转map并且分组

Map<String, List<Student>> listMap = list.stream().collect(Collectors.groupingBy(Student::getSex))
根据对象某些属性,进行分组

Map<List, List> studentsMap= list.stream()

.collect(Collectors.groupingBy(f -> Arrays.asList(f.getAge),f.getSex())));

map转list

List<Student> collect = studentMap.values().stream().collect(Collectors.toList());

6、过滤属性为空的字段

Student s6 = new Student("",30,"男");
list.add(s6);
 
List<String> stringList = list.stream().map(s -> s.getName()).filter(s -> !s.isEmpty()).collect(Collectors.toList());

7、根据某一属性进行计算

根据年龄求最大值、最小值、平均值、总和、个数

IntSummaryStatistics resultNum = list.stream().mapToInt((item)->item.getAge()).summaryStatistics();
System.out.println("max:"+resultNum.getMax());
System.out.println("min:"+resultNum.getMin());
System.out.println("sum:"+resultNum.getSum());
System.out.println("average:"+resultNum.getAverage());
System.out.println("count:"+resultNum.getCount());
1、求和有三种类型,mapToInt,mapToLong,mapToDouble

2、如果是Bigdecimal数值类型,则计算方法如下,(新建对象)

Frult frult1 = new Frult("西瓜",new BigDecimal(1));
Frult frult2 = new Frult("梨子",new BigDecimal(2));
List<Frult> frultList = new ArrayList<>();
frultList.add(frult1);
frultList.add(frult2);
BigDecimal totalPrice = frultList.stream().map(Frult::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
//或者用mapToInt()进行强转(int->Bigdecimal)
  • 24
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值