新公司里面用了很多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