List<Integer> collect = ListUtil.of(1, 2, 3, 1, 2).stream().filter(Objects::nonNull).distinct().collect(Collectors.toList());
System.out.println(collect);
List<String> collect1 = ListUtil.of("user1", "user1", "user2").stream().filter(Objects::nonNull).distinct().collect(Collectors.toList());
System.out.println(collect1);
输出结果:
[1, 2, 3]
[user1, user2]
根据一个属性
List<User> userList = new ArrayList<>(Arrays.asList(
new User(1L,new BigDecimal("10"),18),
new User(1L,new BigDecimal("20"),19),
new User(2L,new BigDecimal("100"),18)));
System.out.println("去重前"+userList);
userList = userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getId()))), ArrayList::new));
System.out.println("去重后"+userList);
输出结果:
去重前[User{id=1, money=10}, User{id=1, money=20}, User{id=2, money=100}]
去重后[User{id=1, money=10}, User{id=2, money=100}]
根据多个属性
List<User> userList = new ArrayList<>(Arrays.asList(
new User(1L,new BigDecimal("10"),18),
new User(1L,new BigDecimal("20"),19),
new User(2L,new BigDecimal("100"),18)));
System.out.println("去重前"+userList);
userList = userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getId() + ";" + o.getAge()))), ArrayList::new));
System.out.println("去重后"+userList);
输出结果:
去重前[User{id=1, money=10, age=18}, User{id=2, money=20, age=19}, User{id=3, money=100, age=17}]
去重后[User{id=1, money=10, age=18}, User{id=2, money=20, age=19}, User{id=3, money=100, age=17}]
List<User> userList = new ArrayList<>(Arrays.asList(
new User(1L,new BigDecimal("10"),18),
new User(2L,new BigDecimal("20"),19),
new User(3L,new BigDecimal("100"),17)));
System.out.println("排序前"+userList);
userList = userList.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());
System.out.println("排序后"+userList);
输出结果:
排序前[User{id=1, money=10, age=18}, User{id=2, money=20, age=19}, User{id=3, money=100, age=17}]
排序后[User{id=3, money=100, age=17}, User{id=1, money=10, age=18}, User{id=2, money=20, age=19}]
List<User> userList = new ArrayList<>(Arrays.asList(
new User(1L,new BigDecimal("10"),18),
new User(2L,new BigDecimal("20"),19),
new User(3L,new BigDecimal("100"),17)));
System.out.println("排序前"+userList);
userList = userList.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList());
System.out.println("排序后"+userList);
输出结果:
排序前[User{id=1, money=10, age=18}, User{id=2, money=20, age=19}, User{id=3, money=100, age=17}]
排序后[User{id=2, money=20, age=19}, User{id=1, money=10, age=18}, User{id=3, money=100, age=17}]
List<User> userList = new ArrayList<>(Arrays.asList(
new User(1L,new BigDecimal("10"),18),
new User(2L,new BigDecimal("20"),19),
new User(3L,new BigDecimal("100"),18)));
System.out.println("排序前"+userList);
userList = userList.stream().sorted(Comparator.comparing(User::getAge).thenComparing(User::getId)).collect(Collectors.toList());
System.out.println("排序后"+userList);
输出结果:
排序前[User{id=1, money=10, age=18}, User{id=2, money=20, age=19}, User{id=3, money=100, age=18}]
排序后[User{id=1, money=10, age=18}, User{id=3, money=100, age=18}, User{id=2, money=20, age=19}]
//根据时间进行升序排序
Map<String, String> map = new HashMap<>();
Map<String, String> result = new LinkedHashMap<>();
map.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(x -> result.put(DateUtil.timeStamp2Date(x.getKey(), null), x.getValue()));
//根据时间进行降序排序
Map<String, String> map = new HashMap<>();
Map<String, String> result = new LinkedHashMap<>();
map.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByKey())).forEachOrdered(x -> result.put(DateUtil.timeStamp2Date(x.getKey(), null), x.getValue()));
List<User> userList = new ArrayList<>(Arrays.asList(
new User(1L,new BigDecimal("10"),18),
new User(2L,new BigDecimal("20"),19),
new User(3L,new BigDecimal("100"),18)));
Map<Integer, List<User>> collect = userList.stream().filter(Objects::nonNull).collect(Collectors.groupingBy(User::getAge));
System.out.println(collect);
输出结果:
{18=[User{id=1, money=10, age=18}, User{id=3, money=100, age=18}], 19=[User{id=2, money=20, age=19}]}
List<User> userList = new ArrayList<>(Arrays.asList(
new User(1L,new BigDecimal("10"),18),
new User(2L,new BigDecimal("20"),19),
new User(3L,new BigDecimal("100"),18)));
Map<Integer, Map<BigDecimal, List<User>>> collect = userList.stream().filter(Objects::nonNull)
.collect(Collectors.groupingBy(User::getAge, Collectors.groupingBy(User::getMoney)));
System.out.println(JSONUtil.toJsonStr(collect));
输出结果:
List<User> userList = new ArrayList<>(Arrays.asList(
new User(1L,new BigDecimal("10"),18),
new User(2L,new BigDecimal("20"),19),
new User(3L,new BigDecimal("100"),18)));
Map<Integer, Map<BigDecimal, List<Long>>> collect = userList.stream().filter(Objects::nonNull)
.collect(Collectors.groupingBy(User::getAge, Collectors.groupingBy(User::getMoney, Collectors.mapping(User::getId, Collectors.toList()))));
System.out.println(JSONUtil.toJsonStr(collect));
输出结果: