jdk8中list的stream流的常用方法

一.判断List对象中是否包含对象的值

1.第一种方法:

boolean present1 = appCountStudentActionResults.stream().filter(m -> m.getActiveTypes().equals("3")).findAny().isPresent();

2.第二种方法:

boolean b = appCountStudentActionResults.stream().anyMatch(m -> m.getActiveTypes().equals("1"))

二.List按某个字段排序

1.第一种方法<Bean>:

​
List<User> newList = list.stream().sorted(Comparator.comparing(User::getAge))
                .collect(Collectors.toList());




List<类> list; 代表某集合
 
//返回 对象集合以类属性一升序排序
 
list.stream().sorted(Comparator.comparing(类::属性一));
 
//返回 对象集合以类属性一降序排序 注意两种写法
 
list.stream().sorted(Comparator.comparing(类::属性一).reversed());//先以属性一升序,结果进行属性一降序
 
list.stream().sorted(Comparator.comparing(类::属性一,Comparator.reverseOrder()));//以属性一降序
(多个属性排序)
原文链接:https://blog.csdn.net/qq_41999771/article/details/123045488

​

2.第二种方法<Map<String,Object>>:

常见排序

public static void main(String args[]) throws IOException, ClassNotFoundException {
    testSorted(); 
}
 
public static void testSorted() {
    List<Map<String, Object>> cpuRateList = setCpuRateValue();
 
 
    List<Map<String, Object>> valueList = cpuRateList.stream()
            .sorted(Comparator.comparing(e -> MapUtils.getDouble(e, "value"))).collect(Collectors.toList());
    System.out.println("============排序 ===========");
    valueList.forEach(System.out::println);
    // 倒序
    //        Collections.reverse(valueList);
    List<Map<String, Object>> reverseValueList = cpuRateList.stream()
            .sorted((c1, c2) -> MapUtils.getDouble(c2, "value").compareTo(MapUtils.getDouble(c1, "value"))).collect(Collectors.toList());
    System.out.println("============倒序===========");
    reverseValueList.forEach(System.out::println);
 
}

处理null的情况(如果排序中,值为null了,要怎么处理呢?)

public static void main(String args[]) throws IOException, ClassNotFoundException {     testMapWithNullSorted();
}
 
public static void testMapWithNullSorted() {
    List<Map<String, Object>> cpuRateList = setCpuRateNullValue();
 
    List<Map<String, Object>> filterValueList = cpuRateList.stream().filter(e -> MapUtils.getDouble(e, "value") != null)
            .sorted(Comparator.comparing(e -> MapUtils.getDouble(e, "value"))).collect(Collectors.toList());
    System.out.println("==========过滤null===========");
    filterValueList.forEach(System.out::println);
 
    List<Map<String, Object>> valueNullFirstList = cpuRateList.stream().sorted(Comparator.comparing(e -> MapUtils.getDouble(e, "value"), Comparator.nullsFirst(Double::compareTo))).collect(Collectors.toList());
    System.out.println("==========null排前面===========");
    valueNullFirstList.forEach(System.out::println);
 
    List<Map<String, Object>> valueNullLastList = cpuRateList.stream().sorted(Comparator.comparing(e -> MapUtils.getDouble(e, "value"), Comparator.nullsLast(Double::compareTo))).collect(Collectors.toList());
    System.out.println("==========null排后面===========");
    valueNullLastList.forEach(System.out::println);
}

JSONArray的排序同理(JSONArray排序)

public static void main(String args[]){
    testSorted();
    testJsonWithNullSorted();
}
 
public static void testSorted() {
    JSONArray fieldList = initFiledValue();
 
    List<JSONObject> sortIdList = ListUtils.emptyIfNull(fieldList).stream().map(e -> (JSONObject) e)
            .sorted(Comparator.comparing(e -> MapUtils.getInteger(e, "sortId"))).collect(Collectors.toList());
    System.out.println("============排序 ===========");
    sortIdList.forEach(System.out::println);
    // 倒序
    List<JSONObject> reverseSortIdList = ListUtils.emptyIfNull(fieldList).stream().map(e -> (JSONObject) e)
            .sorted((c1, c2) -> MapUtils.getInteger(c2, "sortId").compareTo(MapUtils.getInteger(c1, "sortId"))).collect(Collectors.toList());
    System.out.println("============倒序===========");
    reverseSortIdList.forEach(System.out::println);
}
 
public static void testJsonWithNullSorted() {
    JSONArray fieldList = initFiledNullValue();
 
    List<JSONObject> filterValueList = ListUtils.emptyIfNull(fieldList).stream().map(e -> (JSONObject) e).filter(e -> MapUtils.getInteger(e, "sortId") != null)
            .sorted(Comparator.comparing(e -> MapUtils.getInteger(e, "sortId"))).collect(Collectors.toList());
    System.out.println("==========过滤null===========");
    filterValueList.forEach(System.out::println);
 
    List<JSONObject> valueNullFirstList = ListUtils.emptyIfNull(fieldList).stream().map(e -> (JSONObject) e).sorted(Comparator.comparing(e -> MapUtils.getInteger(e, "sortId"), Comparator.nullsFirst(Integer::compareTo))).collect(Collectors.toList());
    System.out.println("==========null排前面===========");
    valueNullFirstList.forEach(System.out::println);
 
    List<JSONObject> valueNullLastList = ListUtils.emptyIfNull(fieldList).stream().map(e -> (JSONObject) e).sorted(Comparator.comparing(e -> MapUtils.getInteger(e, "sortId"), Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList());
    System.out.println("==========null排后面===========");
    valueNullLastList.forEach(System.out::println);
}
//
public static JSONArray initFiledValue() {
    String data = "[{\n" +
            "\t\"fieldName\": \"model_name\",\n" +
            "\t\"sortId\": 2,\n" +
            "\t\"elementLabel\": \"模型名\"\n" +
            "}, {\n" +
            "\t\"elementName\": \"model_mod\",\n" +
            "\t\"sortId\": 1,\n" +
            "\t\"elementLabel\": \"模型ID\"\n" +
            "}, {\n" +
            "\t\"fieldName\": \"remark\", \n" +
            "\t\"sortId\": 3,\n" +
            "\t\"elementLabel\": \"备注\"\n" +
            "}, {\n" +
            "\t\"fieldName\": \"model_type\", \n" +
            "\t\"sortId\": 4,\n" +
            "\t\"elementLabel\": \"类型\" \n" +
            "}]";
    return JSONArray.parseArray(data);
}
 
public static JSONArray initFiledNullValue() {
    String data = "[{\n" +
            "\t\"fieldName\": \"model_name\",\n" +
            "\t\"sortId\": 2,\n" +
            "\t\"elementLabel\": \"模型名\"\n" +
            "}, {\n" +
            "\t\"elementName\": \"model_mod\",\n" +
            "\t\"sortId\": null,\n" +
            "\t\"elementLabel\": \"模型ID\"\n" +
            "}, {\n" +
            "\t\"fieldName\": \"remark\", \n" +
            "\t\"sortId\": 3,\n" +
            "\t\"elementLabel\": \"备注\"\n" +
            "}, {\n" +
            "\t\"fieldName\": \"model_type\", \n" +
            "\t\"sortId\": 4,\n" +
            "\t\"elementLabel\": \"类型\" \n" +
            "}]";
    return JSONArray.parseArray(data);
}

三.Java对list多个字段进行去重以及过滤

1.对年龄和名字同时进行去重(多个字段)

		 public static void main(String[] args) {
				        us u=fa.fa(1);
				        us u2=fa.fa(1);
				        us u3=fa.fa(1);
				        List<us> list=new ArrayList<>();
				        u.setAge(10);
				        u.setName("张三");
				        list.add(u);
				        u2.setAge(13);
				        u2.setName("李四");
				        list.add(u2);
				        u3.setAge(13);
				        u3.setName("李四");
				        list.add(u3);
				        List<us> usList=list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(s->s.getName()+";"+s.getAge()))),ArrayList::new));
				        usList.forEach(userList->{
				        System.out.println(userList.getName()+userList.getAge());
				        });
				    }

2.对年龄进行过滤筛选(单个字段)

 List<us> userDiscont=list.stream().filter(us -> us.getAge()==10).collect(Collectors.toList());



List<Map<String,Object>> list2 = selectChoasInfo.stream().distinct().collect(Collectors.toList());
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值