Lambda小练习

新公司里面用了很多lambda表达式,作为一个以前只撸过视频得人来说,写起来有些费力,就抽空自己练了练。
这是实体类结构。

	private String id;
    private String name;
    private int age;
    private Date birthday;
    private Double totalPoints;
    private String classNo;

自己得练习代码。

 public static void main(String[] args) {
        List<User> userList = new ArrayList<>();
        User user1 = new User("001", "小花", 12, new Date(), 162.32, "20-01");
        User user2 = new User("002", "小明", 13, new Date(), 165.26, "20-02");
        User user3 = new User("003", "小豪", 11, new Date(), 151.12, "20-01");
        User user4 = new User("004", "小智", 14, new Date(), 173.43, "20-03");
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);
        List<String> NameList = userList.stream().map(User::getName).collect(Collectors.toList());
        System.out.println("   NameList : " + NameList.toString());
        System.out.println("----------------------------------------------------------------------------------------");
        System.out.println("   Max Age : " + userList.stream().map(User::getAge).max(Integer::compareTo).get());
        System.out.println("----------------------------------------------------------------------------------------");
        List<String> AgeList = userList.stream().filter(User -> {
            return User.getAge() > 12;
        }).map(User::getName).collect(Collectors.toList());
        System.out.println("   Age > 12 : " + AgeList.toString());
        System.out.println("----------------------------------------------------------------------------------------");
        List<String> SortList = userList.stream().sorted(Comparator.comparing(User::getAge).reversed()).map(User::getName).collect(Collectors.toList());
        System.out.println("   Sort By Age : " + SortList.toString());
        System.out.println("----------------------------------------------------------------------------------------");
        Map<String, List<User>> classMap = userList.stream().collect(Collectors.groupingBy(User::getClassNo));
        System.out.println("   Group By Class : " + classMap.toString());
        System.out.println("----------------------------------------------------------------------------------------");
        Double TotalScope = userList.stream().map(User::getTotalPoints).reduce(0.00, Double::sum);
        System.out.println("   Average Score : " + TotalScope / userList.size());
        System.out.println("----------------------------------------------------------------------------------------");

    }

输出后得结果。

   NameList : [小花, 小明, 小豪, 小智]
----------------------------------------------------------------------------------------
   Max Age : 14
----------------------------------------------------------------------------------------
   Age > 12 : [小明, 小智]
----------------------------------------------------------------------------------------
   Sort By Age : [小智, 小明, 小花, 小豪]
----------------------------------------------------------------------------------------
   Group By Class : {20-03=[po.User@30dae81], 20-02=[po.User@1b2c6ec2], 20-01=[po.User@4edde6e5, po.User@70177ecd]}
----------------------------------------------------------------------------------------
   Average Score : 163.0325
----------------------------------------------------------------------------------------

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值