1,按天分组
// 先按天分组
List<User> list = UserService.list(queryWrapper);
Map<String, List<User>> map = list.stream().collect(Collectors.groupingBy(item -> new SimpleDateFormat("yyyy-MM-dd").format(item.getCreateTime())));
// 然后再遍历自定义输出的格式
List<HashMap<String, Object>> mapList = new ArrayList<>();
for (Map.Entry<String, List<CqtClassroomSyn>> entry : map.entrySet()) {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("time",entry.getKey());
hashMap.put("data",entry.getValue());
mapList.add(hashMap);
}
return mapList;
2,按照某个字段排序
1,方式一
// 只有升序排列
List<User> userAscList = userList.stream().sorted(Comparator.comparing(User::getSort)).collect(Collectors.toList());
// 然后再倒序排列
Collections.reverse(userAscList);
return userAscList;
2,方式二
// 正序排列
Collections.sort(userList, new Comparator<User>() {
@Override
public int compare(User u1, User u2) {
return u1.getTime().compareTo(u2.getTime());
}
});
return userList;
// 倒序排列
Collections.sort(userList, new Comparator<User>() {
@Override
public int compare(User u1, User u2) {
// 只需加一个负号
return -u1.getTime().compareTo(u2.getTime());
}
});
return userList;
3,示例
@Data
public class Logs implements Serializable, Comparable<Logs> {
private String id;
private String username;
private String contents;
private String consequence;
private String ip;
private Long times;
@Override
public int compareTo(Logs o) {
return 0;
}
}
@Test
public void testMath(){
List<Logs> list = new ArrayList<Logs>();
list.add(new Logs("1","chen", "djifand", "jdifa", "jdfid", 49L));
list.add(new Logs("134","ce", "cae", "jdifa", "jdfid", 123L));
list.add(new Logs("3","cet", "djifand", "jdifa", "jdfid", 99L));
list.add(new Logs("4","egge", "djifand", "jdifa", "jdfid", 111L));
list.add(new Logs("15","chen", "djifand", "jdifa", "jdfid", 59L));
list.add(new Logs("6","chen2", "cae", "jdifa", "jdfid", 123L));
list.add(new Logs("87","cet", "djifand", "jdifa", "jdfid", 213L));
list.add(new Logs("8","egge", "djifand", "jdifa", "jdfid", 201L));
list.add(new Logs("9","egge2", "djifand", "jdifa", "jdfid", 269L));
list.add(new Logs("10","ce", "cae", "jdifa", "jdfid", 121L));
list.add(new Logs("11","chen3", "djifand", "jdifa", "jdfid", 1123L));
list.add(new Logs("5","cet2", "djifand", "jdifa", "jdfid", 12L));
System.out.println("---- 排序前的输出 ----- ");
for (Logs logs : list) {
System.out.println("数据: "+logs);
}
System.out.println("---- 排序后的结果 ----- ");
List<Logs> search = search(list);
for (Logs logs : search) {
System.out.println("-->"+logs);
}
}
// 方式1: 降序排列
public List<Logs> search(List<Logs> logsList){
Collections.sort(logsList, new Comparator<Logs>() {
@Override
public int compare(Logs o1, Logs o2) {
if ((o1.getTimes() > o2.getTimes())){
return -1;
}
if (o1.getTimes() == o2.getTimes()){
return 0;
}
return 1;
}
});
return logsList;
}
// 方式2: 升序排列
public List<Logs> search(List<Logs> logsList){
Collections.sort(logsList, new Comparator<Logs>() {
@Override
public int compare(Logs o1, Logs o2) {
if ((o1.getTimes() > o2.getTimes())){
return 1;
}
if (o1.getTimes() == o2.getTimes()){
return 0;
}
return -1;
}
});
return logsList;
}
// 方式3: 降序排列
public List<Logs> search(List<Logs> logsList){
Collections.sort(logsList, new Comparator<Logs>() {
@Override
public int compare(Logs o1, Logs o2) {
return -o1.getTimes().compareTo(o2.getTimes());
}
});
return logsList;
}
// 方式4: 升序排列
public List<Logs> search(List<Logs> logsList){
Collections.sort(logsList, new Comparator<Logs>() {
@Override
public int compare(Logs o1, Logs o2) {
return o1.getTimes().compareTo(o2.getTimes());
}
});
return logsList;
}
4,其他排序方法
// 查询数据库数据
List list = UserServer.getList();
// 去重,去除重复对象(每个属性的值都一样的),需要注意的是要先重写对象User的equals和hashCode方法
List distinctList = list.stream().distinct().collect(Collectors.toList());
// 排序,按id升续排列,如果要降续则改成:(a,b)->b.getId()-a.getId();a和b都是变量名(可以按自己意愿取名字),都是list中的对象的实例
List sortList = list.stream().sorted((a,b) -> a.getId() - b.getId()).collect(Collectors.toList());
// 过滤,按照自己的需求来筛选list中的数据,比如我筛选出不及格的(小于60分)的人,t为实例
List filterList = list.stream().filter(t -> t.getScore() < 60).collect(Collectors.toList());
// map,提取对象中的某一元素,例子中我取的是每个人的name,注意list中类型对应,如果取的是id或者班级,就应该是integer类型
List mapList = list.stream().map(t -> t.getName()).collect(Collectors.toList());
// 统计,统计所有人分数的和,主要我设置的分数属性是double类型的,所以用mapToDouble,如果是int类型的,则需要用mapToInt
double sum = list.stream().mapToDouble(t -> t.getScore()).sum();
int count = list.stream().mapToInt(t -> t.getId()).sum();
// 分组,按照字段中某个属性将list分组
Map> map = list.stream().collect(Collectors.groupingBy(t -> t.getGrade()));
// 然后再对map处理,这样就方便取出自己要的数据
for(Map.Entry> entry : map.entrySet()){
System.out.println("key:"+entry.getKey());
System.out.println("value:"+entry.getValue());
}
// 多重分组,先按年级分组,再按班级分组
groupMap = list.stream().collect(Collectors.groupingBy(t -> t.getGrade(), Collectors.groupingBy(t -> t.getClasses())));
// 多重分组,一般多重分组后都是为了统计,比如说统计每个年级,每个班的总分数
sumMap = list.stream().collect(Collectors.groupingBy(t -> t.getGrade(), Collectors.groupingBy(t -> t.getClasses(), Collectors.summingDouble(t -> t.getScore()))));
// stream是链式的,这些功能是可以一起使用的,例如:计算每个年级每个班的及格人数
integerMap = list.stream().filter(t -> t.getScore() >= 60).collect(Collectors.groupingBy(t -> t.getGrade(), Collectors.groupingBy(t -> t.getClasses(), Collectors.counting())));