1,按天分组

  1. // 先按天分组
  2. List<User> list = UserService.list(queryWrapper);
  3. Map<String, List<User>> map = list.stream().collect(Collectors.groupingBy(item -> new SimpleDateFormat("yyyy-MM-dd").format(item.getCreateTime())));
  4. // 然后再遍历自定义输出的格式
  5. List<HashMap<String, Object>> mapList = new ArrayList<>();
  6. for (Map.Entry<String, List<CqtClassroomSyn>> entry : map.entrySet()) {
  7. HashMap<String, Object> hashMap = new HashMap<>();
  8. hashMap.put("time",entry.getKey());
  9. hashMap.put("data",entry.getValue());
  10. mapList.add(hashMap);
  11. }
  12. return mapList;

2,按照某个字段排序

1,方式一

  1. // 只有升序排列
  2. List<User> userAscList = userList.stream().sorted(Comparator.comparing(User::getSort)).collect(Collectors.toList());
  3. // 然后再倒序排列
  4. Collections.reverse(userAscList);
  5. return userAscList;

2,方式二

  1. // 正序排列
  2. Collections.sort(userList, new Comparator<User>() {
  3. @Override
  4. public int compare(User u1, User u2) {
  5. return u1.getTime().compareTo(u2.getTime());
  6. }
  7. });
  8. return userList;
  9. // 倒序排列
  10. Collections.sort(userList, new Comparator<User>() {
  11. @Override
  12. public int compare(User u1, User u2) {
  13. // 只需加一个负号
  14. return -u1.getTime().compareTo(u2.getTime());
  15. }
  16. });
  17. return userList;

3,示例

  1. @Data
  2. public class Logs implements Serializable, Comparable<Logs> {
  3. private String id;
  4. private String username;
  5. private String contents;
  6. private String consequence;
  7. private String ip;
  8. private Long times;
  9. @Override
  10. public int compareTo(Logs o) {
  11. return 0;
  12. }
  13. }
  1. @Test
  2. public void testMath(){
  3. List<Logs> list = new ArrayList<Logs>();
  4. list.add(new Logs("1","chen", "djifand", "jdifa", "jdfid", 49L));
  5. list.add(new Logs("134","ce", "cae", "jdifa", "jdfid", 123L));
  6. list.add(new Logs("3","cet", "djifand", "jdifa", "jdfid", 99L));
  7. list.add(new Logs("4","egge", "djifand", "jdifa", "jdfid", 111L));
  8. list.add(new Logs("15","chen", "djifand", "jdifa", "jdfid", 59L));
  9. list.add(new Logs("6","chen2", "cae", "jdifa", "jdfid", 123L));
  10. list.add(new Logs("87","cet", "djifand", "jdifa", "jdfid", 213L));
  11. list.add(new Logs("8","egge", "djifand", "jdifa", "jdfid", 201L));
  12. list.add(new Logs("9","egge2", "djifand", "jdifa", "jdfid", 269L));
  13. list.add(new Logs("10","ce", "cae", "jdifa", "jdfid", 121L));
  14. list.add(new Logs("11","chen3", "djifand", "jdifa", "jdfid", 1123L));
  15. list.add(new Logs("5","cet2", "djifand", "jdifa", "jdfid", 12L));
  16. System.out.println("---- 排序前的输出 ----- ");
  17. for (Logs logs : list) {
  18. System.out.println("数据: "+logs);
  19. }
  20. System.out.println("---- 排序后的结果 ----- ");
  21. List<Logs> search = search(list);
  22. for (Logs logs : search) {
  23. System.out.println("-->"+logs);
  24. }
  25. }
  26. // 方式1: 降序排列
  27. public List<Logs> search(List<Logs> logsList){
  28. Collections.sort(logsList, new Comparator<Logs>() {
  29. @Override
  30. public int compare(Logs o1, Logs o2) {
  31. if ((o1.getTimes() > o2.getTimes())){
  32. return -1;
  33. }
  34. if (o1.getTimes() == o2.getTimes()){
  35. return 0;
  36. }
  37. return 1;
  38. }
  39. });
  40. return logsList;
  41. }
  42. // 方式2: 升序排列
  43. public List<Logs> search(List<Logs> logsList){
  44. Collections.sort(logsList, new Comparator<Logs>() {
  45. @Override
  46. public int compare(Logs o1, Logs o2) {
  47. if ((o1.getTimes() > o2.getTimes())){
  48. return 1;
  49. }
  50. if (o1.getTimes() == o2.getTimes()){
  51. return 0;
  52. }
  53. return -1;
  54. }
  55. });
  56. return logsList;
  57. }
  58. // 方式3: 降序排列
  59. public List<Logs> search(List<Logs> logsList){
  60. Collections.sort(logsList, new Comparator<Logs>() {
  61. @Override
  62. public int compare(Logs o1, Logs o2) {
  63. return -o1.getTimes().compareTo(o2.getTimes());
  64. }
  65. });
  66. return logsList;
  67. }
  68. // 方式4: 升序排列
  69. public List<Logs> search(List<Logs> logsList){
  70. Collections.sort(logsList, new Comparator<Logs>() {
  71. @Override
  72. public int compare(Logs o1, Logs o2) {
  73. return o1.getTimes().compareTo(o2.getTimes());
  74. }
  75. });
  76. return logsList;
  77. }

4,其他排序方法

  1. // 查询数据库数据
  2. List list = UserServer.getList();
  3. // 去重,去除重复对象(每个属性的值都一样的),需要注意的是要先重写对象User的equals和hashCode方法
  4. List distinctList = list.stream().distinct().collect(Collectors.toList());
  5. // 排序,按id升续排列,如果要降续则改成:(a,b)->b.getId()-a.getId();a和b都是变量名(可以按自己意愿取名字),都是list中的对象的实例
  6. List sortList = list.stream().sorted((a,b) -> a.getId() - b.getId()).collect(Collectors.toList());
  7. // 过滤,按照自己的需求来筛选list中的数据,比如我筛选出不及格的(小于60分)的人,t为实例
  8. List filterList = list.stream().filter(t -> t.getScore() < 60).collect(Collectors.toList());
  9. // map,提取对象中的某一元素,例子中我取的是每个人的name,注意list中类型对应,如果取的是id或者班级,就应该是integer类型
  10. List mapList = list.stream().map(t -> t.getName()).collect(Collectors.toList());
  11. // 统计,统计所有人分数的和,主要我设置的分数属性是double类型的,所以用mapToDouble,如果是int类型的,则需要用mapToInt
  12. double sum = list.stream().mapToDouble(t -> t.getScore()).sum();
  13. int count = list.stream().mapToInt(t -> t.getId()).sum();
  14. // 分组,按照字段中某个属性将list分组
  15. Map> map = list.stream().collect(Collectors.groupingBy(t -> t.getGrade()));
  16. // 然后再对map处理,这样就方便取出自己要的数据
  17. for(Map.Entry> entry : map.entrySet()){
  18. System.out.println("key:"+entry.getKey());
  19. System.out.println("value:"+entry.getValue());
  20. }
  21. // 多重分组,先按年级分组,再按班级分组
  22. groupMap = list.stream().collect(Collectors.groupingBy(t -> t.getGrade(), Collectors.groupingBy(t -> t.getClasses())));
  23. // 多重分组,一般多重分组后都是为了统计,比如说统计每个年级,每个班的总分数
  24. sumMap = list.stream().collect(Collectors.groupingBy(t -> t.getGrade(), Collectors.groupingBy(t -> t.getClasses(), Collectors.summingDouble(t -> t.getScore()))));
  25. // stream是链式的,这些功能是可以一起使用的,例如:计算每个年级每个班的及格人数
  26. integerMap = list.stream().filter(t -> t.getScore() >= 60).collect(Collectors.groupingBy(t -> t.getGrade(), Collectors.groupingBy(t -> t.getClasses(), Collectors.counting())));