java8特性 对集合的 常规操作

分组、去重、排序、求和、过滤、集合拷贝、过滤、等

List 集合

package com.xxx.xxx.service.alarm.impl;

/**
 * @Author: xzj
 * @DateTime: 2022/1/18 11:13
 * @Description: TODO
 */
 @Data
public class Demo {

    private String id;

    private String name;

    private String age;

    private String rowIndex;


    public Demo(String id, String name, String age, String rowIndex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.rowIndex = rowIndex;
    }

      @Override
    public String toString() {
        return "Demo{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", rowIndex='" + rowIndex + '\'' +
                '}';
    }



        List<Demo> allData = Arrays.asList(
                new Demo("1", "2", "30", null),
                new Demo("2", "1", "20", null),
                new Demo("2", "1", "20", null),
                new Demo("3", "xzj", "20", null));

去重

       // 去重
        List<Demo> demo1 = allData.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(
                                Comparator.comparing(data -> data.getAge()))), ArrayList::new));
        System.out.println(userList);

// 两个集合去重
    List collect = Stream.of(parseArray, pmNumbers)
                    .flatMap(Collection::stream)
                    .distinct()
                    .collect(Collectors.toList());

取差集

            List<PmNumber> collect = list1.stream().filter(e -> {
                return !list2.contains(e);
            }).collect(Collectors.toList());

过滤(根据某一属性)

   List<Demo> demo2 =allData.stream().filter(e -> e.getAge().equals(30)).collect(toList());
   // 过滤为空
   List<Demo> demo = cffjzhbDataList.stream().filter(e -> StringUtils.isNotEmpty(e.getGx()) && StringUtils.isNotEmpty(e.getClf())).collect(toList());

分组(根据某一属性)

             
 /**
 *根据一个属性分组
 */
   Map<String, List<Demo>> groupBySex = allData
                .stream().collect(Collectors.groupingBy(Demo::getAge));
               
             
 /**
 *根据多个属性分组
 */
Map<String, List<Demo>> groupBySex = allData
                .stream().collect(Collectors.groupingBy(e -> e.getName() + "_" + e.getAge()));


// 第二种方法不建议使用(多种属性分组)
       Function<Demo, List<Object>> compositeKey = allData->
                Arrays.<Object>asList(user.getAge(), user.getName());
        Map<Object, List<Demo>> map =
                allData.stream().collect(Collectors.groupingBy(compositeKey, Collectors.toList()));

排序 (单字段排序,多字段排序)

// 自然排序
list.stream().sorted() 
// 自然序逆序元素,使用Comparator 提供的reverseOrder() 方法
list.stream().sorted(Comparator.reverseOrder())

       //正序 123
        List<Demo> a = allData.stream().sorted(Comparator.comparing(Demo::getName)).collect(toList());
        //倒序 321
        List<Demo> b = allData.stream().sorted(Comparator.comparing(Demo::getName).reversed()
        ).collect(toList());


        // 多条件排序
        List<Demo> sortList = allData.stream()

                .sorted(Comparator.comparing(Demo::getName, Comparator.reverseOrder())
                        .thenComparing(Demo::getAge)
                        .thenComparing(Demo::getId, Comparator.reverseOrder()))
                .collect(Collectors.toList());

求和、最大、最小、平均值计算

DecimalFormat df = new DecimalFormat("0.00");//保留两位小数点
        double sum = allData.stream().mapToDouble(Demo::getAge).sum();
        System.out.println("身高 总和:" + df.format(sum));
        double max = allData.stream().mapToDouble(Demo::getHeight).max().getAsDouble();
        System.out.println("身高 最大:" + df.format(max));
        double min = allData.stream().mapToDouble(Demo::getHeight).min().getAsDouble();
        System.out.println("身高 最小:" + df.format(min));
        double average = allData.stream().mapToDouble(Demo::getHeight).average().getAsDouble();
        System.out.println("身高 平均:" + df.format(average));

a对象的内容拷贝到b

        List<Demo> copyDemo = allData.stream().map(p -> {
            Demo e = new Demo();
            e.setAge(p.getAge());
            e.setName(p.getName());
            return e;
        }).collect(Collectors.toList());

集合中属性拼接 (取出集合中名字属性并拼接)

        String stationCodes = " in ('" + allData.stream().map(x -> x.getName()).distinct().collect(Collectors.joining("','")) + "')";

在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值