java Lambda表达式

Posted lzxc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Lambda表达式相关的知识,希望对你有一定的参考价值。

 计算集合 List<RepaymentList> repaymentLists 中 RepaymentList 类中 UnityStatus 状态为1、6、7的属性 Principal 的值之和 

new BigDecimal()  将String 类型转成BigDecimal类型

//计算集合 List<RepaymentList> repaymentLists 中 RepaymentList 类中 UnityStatus 状态为1、6、7的属性 Principal 的值之和 
bigDecimal = repaymentLists.stream().filter(r -> "1".equals(r.getUnityStatus()) || "6".equals(r.getUnityStatus()) || "7".equals(r.getUnityStatus())).map(r -> new BigDecimal(r.getPrincipal())).reduce(BigDecimal.ZERO, BigDecimal::add);

 

利用  stream().map() 给list赋值 

public static List<String> StreamMap(){
        List<String> list1=new ArrayList<>();
        List<String> list2=new ArrayList<>();
        list1.add("1");
        list1.add("2");
        list1.add("3");
        //利用stream().map()给List2赋值
        //list2=list1.stream().map(a->{return "stream().map()处理之后:"+ a;}).collect(Collectors.toList());
        list2=list1.stream().map(a->{return a;}).collect(Collectors.toList());//利用stream().map()给List2赋值
        //list2.stream().forEach(a->{System.out.print(a);});
        return list2;
    }

 

利用 stream().filter()过虑list

/**
     * 利用stream().filter()过虑list
     */
    public static void StreamFilter(){
        List<String> list=new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("d");
        // 获取list中值不为"d"的所有元素
        List<String> list1=list.stream().filter(a->a!="d").collect(Collectors.toList());
        System.out.println(list1);
    }

    /**
     * 利用stream().filter()过虑list
     * @return 过虑后的list
     */
    public static List<user> StreamFilterUser(){
        user u=new user(1,"aa","aer3");
        user u1=new user(2,"bbb","423424");
        user u2=new user(3,"ccc","4424324");
        List<user> userList=new ArrayList<>();
        userList.add(u);
        userList.add(u1);
        userList.add(u2);
        // 获取 userList 中 user类中属性id值为2的元素
        List<user> userList1 = userList.stream().filter(a->a.id==2).collect(Collectors.toList());
        return userList1;
    }

    /**
     * 利用stream().filter()过虑list
     * @return 过虑后的list
     */
    public static List<user> streamFilterUserContail(){
        List<String> list = new ArrayList<>();
        list.add("aa");
        list.add("bbb");

        user u=new user(1,"aa","aer3");
        user u1=new user(2,"bbb","423424");
        user u2=new user(3,"ccc","4424324");
        user u3=new user(4,"ddd","43636");
        user u4=new user(5,"eee","634645764");
        List<user> userList=new ArrayList<>();
        userList.add(u);
        userList.add(u1);
        userList.add(u2);
        userList.add(u3);
        userList.add(u4);
        //获取获取 userList 中 user类中属性name的值与list中元素值相同的实体
        List<user> list1=userList.stream().filter(a->{return list.contains(a.getName());}).collect(Collectors.toList());
        return list1;
    }

 

排序

/**
     * 排序
     * @return 排序后的集合
     */
    public static List<user> storeUser(){
        user u=new user(1,"aa","aer3");
        user u1=new user(2,"bbb","423424");
        user u2=new user(3,"ccc","4424324");
        user u3=new user(4,"ddd","43636");
        user u4=new user(5,"eee","634645764");
        user u5=new user(6,"abc","Eefw");
        user u6=new user(7,"bcd","Eefw");
        List<user> userList=new ArrayList<>();
        userList.add(u);
        userList.add(u3);
        userList.add(u4);
        userList.add(u1);
        userList.add(u2);
        userList.add(u5);
        userList.add(u6);
        //userList.sort((a,b)->a.getId()-b.getId());//根据id 顺序排序
        //userList.sort(Comparator.comparing(user::getId));//根据id 顺序排序
        //userList.sort((a,b)->b.getId()-a.getId());//根据id 逆顺排序
        //userList.sort(Comparator.comparing(user::getId).reversed());//根据id逆序排序

        //userList.sort(Comparator.comparing(user::getName).thenComparing(user::getId));//多条件排序
        //userList.stream().filter(a->a.getName()!="aa").collect(Collectors.toList());
        List<user> list1=userList.stream().filter(a->a.getId()==1||a.getId()==3|| a.getId()==5).collect(Collectors.toList());
        list1.sort(Comparator.comparing(user::getId));
        return list1;
    }

 

以上是关于java Lambda表达式的主要内容,如果未能解决你的问题,请参考以下文章

Java的Lambda表达式

Java Lambda表达

聊聊Java 8的Lambda表达式和函数式接口

函数式编程

Java Lambda

Java 8 新特性总结