zoukankan      html  css  js  c++  java
  • 使用JAVA8 filter对List多条件模糊筛选、单个参数多个值过滤、多个参数联合排序

    对于从缓存中获取的list集合如果根据条件进行模糊查询呢?

    条件根据indexName字段筛选需要的值

    1、造数据

    List<ShardsInfoDto> shardsList = new ArrayList<>();
    ShardsInfoDto shardsInfoDto = new ShardsInfoDto();
    shardsInfoDto.setIndexName("chenming20210416");
    shardsInfoDto.setDocs(3);
    shardsInfoDto.setStore(2.0);
    ShardsInfoDto shardsInfoDto1 = new ShardsInfoDto();
    shardsInfoDto1.setIndexName("asdasd");
    shardsInfoDto1.setDocs(1);
    shardsInfoDto1.setStore(4.1);
    ShardsInfoDto shardsInfoDto2 = new ShardsInfoDto();
    shardsInfoDto2.setIndexName("chenmingasdhkask");
    shardsInfoDto2.setDocs(1);
    shardsInfoDto2.setStore(5.1);
    ShardsInfoDto shardsInfoDto3 = new ShardsInfoDto();
    shardsInfoDto3.setIndexName("asdfghjkl");
    shardsInfoDto3.setDocs(4);
    shardsInfoDto3.setStore(3.1);
    ShardsInfoDto shardsInfoDto4 = new ShardsInfoDto();
    shardsInfoDto4.setIndexName("lkjhgfds");
    shardsInfoDto4.setDocs(2);
    shardsInfoDto4.setStore(1.1);
    ShardsInfoDto shardsInfoDto5 = new ShardsInfoDto();
    shardsInfoDto5.setIndexName("erghjcvghui");
    shardsInfoDto5.setDocs(2);
    shardsInfoDto5.setStore(1.2);
    ShardsInfoDto shardsInfoDto6 = new ShardsInfoDto();
    shardsList.add(shardsInfoDto);
    shardsList.add(shardsInfoDto1);
    shardsList.add(shardsInfoDto3);
    shardsList.add(shardsInfoDto4);
    shardsList.add(shardsInfoDto2);
    shardsList.add(shardsInfoDto5);
    shardsList.add(shardsInfoDto6);
    2、模糊查询:

    String indexName = "asd";
    shardsList = shardsList.stream().filter(ShardsInfoDto -> ShardsInfoDto.getIndexName()!=null&&ShardsInfoDto.getIndexName().indexOf(indexName) > -1).collect(Collectors.toList());
    shardsList.stream().forEach(ShardsInfoDto -> {
    System.out.println(ShardsInfoDto.getIndexName());
    });
    ShardsInfoDto.getIndexName()!=null 判断防止空指针异常
    输出结果:

    3、单个参数多个值过滤

    List<String> shardStateList = new ArrayList<>();
    shardStateList.add("a");
    shardStateList.add("b");
    shardStateList.add("c");
    shardsList = shardsList.stream().filter(ShardsInfoDto -> shardStateList.contains(ShardsInfoDto.getShardStates())).collect(Collectors.toList());
    4、多个参数联合排序:

    String docsFlag = "asc";
    String storeFlag = "desc";
    //排序
    Comparator<ShardsInfoDto> doc = null;
    if("asc".equals(docsFlag)){
    //docs升序
    doc = Comparator.nullsFirst(Comparator.comparing(ShardsInfoDto::getDocs, Comparator.nullsFirst(Integer::compareTo)));
    }else if("desc".equals(docsFlag)){
    //docs降序
    doc = Comparator.nullsFirst(Comparator.comparing(ShardsInfoDto::getDocs, Comparator.nullsFirst(Integer::compareTo)).reversed());
    }
    Comparator<ShardsInfoDto> store = null;
    if("asc".equals(storeFlag)){
    //store升序
    store = Comparator.nullsFirst(Comparator.comparing(ShardsInfoDto::getStore, Comparator.nullsFirst(Double::compareTo)));
    }else if("desc".equals(storeFlag)){
    //store降序
    store = Comparator.nullsFirst(Comparator.comparing(ShardsInfoDto::getStore, Comparator.nullsFirst(Double::compareTo)).reversed());
    }
    // 联合排序
    Comparator<ShardsInfoDto> finalComparator = doc.thenComparing(store);
    shardsList.sort(finalComparator);
    for (ShardsInfoDto dto:shardsList) {
    System.out.println("doc:"+dto.getDocs()+",store:"+dto.getStore());
    }
    Comparator.nullsFirst()防止参数为空导致空指针异常!
    结果:


    ————————————————
    版权声明:本文为CSDN博主「东耳佳茗」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_19201215/article/details/115973047

  • 相关阅读:
    nullptr 与 null
    nullptr 与 null
    UNREFERENCED_PARAMETER 的作用
    Kali-Linux无线网络渗透测试-李亚伟-第3章-监听WiFi网络--虚拟机使用无线网卡
    探寻Linux背后的“美丽心灵”——Linux创始人Linus Torvalds访谈录
    Angular2(Beta)入门
    angular2 will be kiiled by react in the future?
    《Linux设备驱动开发详解(第3版)》(即《Linux设备驱动开发详解:基于最新的Linux 4.0内核》)前言
    Angular vs React – so which do I chose?
    2015-2016前端知识体系
  • 原文地址:https://www.cnblogs.com/javalinux/p/15543442.html
Copyright © 2011-2022 走看看