标签:rgs streams collect array bsp lambda style color 写法
1、排序
import lombok.Data; import java.util.*; import java.util.stream.Collectors; public class StreamStudy { public static void main(String[] args) { User u1 = new User("陈一",32); User u2 = new User("陈二",23); User u3 = new User("陈三",33); List<User> users = new ArrayList<>(); users.add(u1); users.add(u2); users.add(u3); List<User> collect1 = users.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList()); System.out.println(collect1); List<User> collect2 = users.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList()); System.out.println(collect2); } } @Data class User { public User() {} public User(String name, int age) { this.name = name; this.age = age; } String name; int age; }
标签:rgs streams collect array bsp lambda style color 写法
原文地址:https://www.cnblogs.com/xiaofengfree/p/12785724.html