实体集合中属性相同的排除

    1. public static List<User> removeDuplicate(List<User> users){
    2. Set<User> set = new TreeSet<>(new Comparator<User>() {
    3. @Override
    4. public int compare(User o1, User o2) {
    5. return o1.getUserName().compareTo(o2.getUserName());
    6. }
    7. });
    8. set.addAll(users);
    9. return new ArrayList<User>(set);
    10. }

    实体集合的groupBy操作

    1. public static void main(String[] args){
    2. List<TestGroup> groupList = new ArrayList<>();
    3. TestGroup g1 = new TestGroup();
    4. g1.setVip("精品");
    5. g1.setOthter("test");
    6. TestGroup g2 = new TestGroup();
    7. g2.setVip("精品");
    8. g2.setOthter("test2");
    9. TestGroup g3 = new TestGroup();
    10. g3.setVip("运营");
    11. g3.setOthter("test2");
    12. TestGroup g4 = new TestGroup();
    13. g4.setVip("高级");
    14. g4.setOthter("test2");
    15. TestGroup g5 = new TestGroup();
    16. g5.setVip("高级");
    17. g5.setOthter("test2");
    18. TestGroup g6 = new TestGroup();
    19. g6.setVip("定制");
    20. g6.setOthter("test2");
    21. TestGroup g7 = new TestGroup();
    22. g7.setVip("定制");
    23. g7.setOthter("test2");
    24. TestGroup g8 = new TestGroup();
    25. g8.setVip("定制");
    26. g8.setOthter("test2");
    27. TestGroup g9 = new TestGroup();
    28. g9.setVip("定制");
    29. g9.setOthter("test2");
    30. List<TestGroup> groups = Arrays.asList(g1,g2,g3,g4,g5,g6,g7,g8,g9);
    31. groupList.addAll(groups);
    32. Map<String,Long> result = groups.stream().
    33. collect(Collectors.groupingBy(TestGroup::getVip,Collectors.counting()));
    34. for(String s:result.keySet()){
    35. System.out.println(s + ":" + result.get(s));
    36. }
    37. }