Java集合排序
List 集合
存储元素不为 null,且排序字段不为 null
public static void main(String[] args) {
List<UserDTO> userDTO = new ArrayList<>();
userDTO.add(new UserDTO(new BigInteger("1"), "c", "s"));
userDTO.add(new UserDTO(new BigInteger("2"), "d", "ss"));
userDTO.add(new UserDTO(new BigInteger("3"), "a", "ss"));
// 匿名内部类排序
Collections.sort(userDTO, new Comparator<UserDTO>() {
@Override
public int compare(UserDTO o1, UserDTO o2) {
return o1.getUsername().compareTo(o2.getUsername());
}
});
// 匿名内部类简化为 Lambda 排序
Collections.sort(userDTO, (UserDTO u1, UserDTO u2) -> u1.getUsername().compareTo(u2.getUsername()));
// Java8中,List 类的 sort() 方法排序,简化为
userDTO.sort((UserDTO u1, UserDTO u2) -> u1.getUsername().compareTo(u2.getUsername()));
// Java8 Lambda 类型推断简化排序
userDTO.sort((u1, u2) -> u1.getUsername().compareTo(u2.getUsername()));
// 静态方法抽取公共 Lambda 表达式
userDTO.sort(UserDTO::compareByUsername);
// Comparator 类 comparing() 方法,将 Function 参数作为比较元素
userDTO.sort(Comparator.comparing(UserDTO::getUsername));
// 多条件排序: Java8 使用 comparing()方法 和 thenComparing() 方法
userDTO.sort(Comparator.comparing(UserDTO::getUsername).thenComparing(UserDTO::getId));
// Java8 Stream sorted()排序
userDTO = userDTO.stream().sorted(Comparator.comparing(UserDTO::getUsername)).collect(Collectors.toList());
// 调转排序判断 倒序
userDTO.sort((u1, u2) -> u2.getUsername().compareTo(u1.getUsername()));
// Comparator reversed() 方法倒序
Comparator<UserDTO> comparator = (u1, u2) -> u1.getUsername().compareTo(u2.getUsername());
userDTO.sort(comparator.reversed());
// Comparator comparing() 方法倒序
userDTO.sort(Comparator.comparing(UserDTO::getUsername, Comparator.reverseOrder()));
// Stream Comparator.reverseOrder()倒序
userDTO.stream().sorted(Comparator.comparing(UserDTO::getUsername, Comparator.reverseOrder())).collect(Collectors.toList());
// 遍历打印
userDTO.forEach(userDTO1 -> System.out.println(userDTO1));
}
存储元素为 null,且排序字段不为 null
public static void main(String[] args) {
List<UserDTO> userDTO = new ArrayList<>();
userDTO.add(new UserDTO(new BigInteger("1"), "c", "s"));
userDTO.add(null);
userDTO.add(new UserDTO(new BigInteger("2"), "d", "ss"));
userDTO.add(new UserDTO(new BigInteger("3"), "a", "ss"));
// Comparator.nullsLast() 排序,null 在结尾 集合存空对象,排序条件的字段不能为 null
userDTO.sort(Comparator.nullsLast(Comparator.comparing(UserDTO::getUsername)));
// Comparator.nullsFirst() 排序,null 在开头 集合存空对象,排序条件的字段不能为 null
userDTO.sort(Comparator.nullsFirst(Comparator.comparing(UserDTO::getUsername)));
// 遍历打印
userDTO.forEach(userDTO1 -> System.out.println(userDTO1));
}
存储元素为 null,且排序字段为 null
public static void main(String[] args) {
List<UserDTO> userDTO = new ArrayList<>();
userDTO.add(new UserDTO(new BigInteger("1"), "c", "s"));
userDTO.add(null);
userDTO.add(new UserDTO(new BigInteger("2"), null, "ss"));
userDTO.add(new UserDTO(new BigInteger("3"), "a", "ss"));
// 排序条件的字段是 null,Comparator 组合
userDTO.sort(Comparator.nullsLast(Comparator.nullsLast(Comparator.comparing(UserDTO::getUsername, Comparator.nullsLast(String::compareTo)))));
// 遍历打印
userDTO.forEach(userDTO1 -> System.out.println(userDTO1));
}
借鉴网址
使用 Lambda 表达式实现超强的排序功能(原创网址)