创建实体对象
public class Work implements Comparable<Work>{
private String computer;
private int num;
private String date;
private String partDate;
public String getPartDate() {
Date d = DateUtils.parseStr2Date(date, "yyyy-MM-dd HH:mm:ss");
String sysDate = DateUtils.dateFormate(d, "yyyy-MM");
return sysDate;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getComputer() {
return computer;
}
public void setComputer(String computer) {
this.computer = computer;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
/**
* 根据日期逆序排列
* @param o
* @return
*/
@Override
public int compareTo(Work o) {
// int flag = this.getSystemDate().compareTo(o.getSystemDate());
int flag = o.getDate().compareTo(this.getDate());
return flag;
}
package com.mq.consume.te;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mq.consume.te.Benfen.Item;
import com.mq.consume.te.Benfen.Work;
import com.mq.consume.te.rab.DateUtils;
import com.mq.consume.te.rab.SHAUtils;
import org.springframework.beans.BeanUtils;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TestS {
public static void main(String[] args) {
List<Work> list=new ArrayList<>();
Work work1=new Work();
work1.setComputer("三星");
work1.setNum(1);
work1.setDate("2020-08-17 12:12:12");
Work work2=new Work();
work2.setComputer("苹果");
work2.setNum(2);
work2.setDate("2020-06-19 12:12:12");
Work work3=new Work();
work3.setComputer("小米");
work3.setNum(3);
work3.setDate("2020-05-18 12:12:12");
Work work4=new Work();
work4.setComputer("华为");
work4.setNum(4);
work4.setDate("2020-05-16 12:12:12");
Work work5=new Work();
work5.setComputer("惠普");
work5.setNum(555555);
work5.setDate("2020-05-16 12:12:12");
Work work6=new Work();
work6.setComputer("惠普");
work6.setNum(4444444);
work6.setDate("2020-05-16 15:12:12");
Work work7=new Work();
work7.setComputer("华为");
work7.setNum(777777);
work7.setDate("2020-05-18 12:12:12");
list.add(work2);
list.add(work4);
list.add(work1);
list.add(work3);
list.add(work5);
list.add(work6);
list.add(work7);
Collections.sort(list);
Map<String, Map<String, List<Work>>> sortMap = new TreeMap<String, Map<String, List<Work>>>(
new Comparator<String>() {
public int compare(String obj1, String obj2) {
// 降序排序
return obj2.compareTo(obj1);
}
});
Map<String, Map<String, List<Work>>> req = new HashMap<>();
//根据时间分组
Map<String, List<Work>> mDate = list.stream().collect(Collectors.groupingBy(Work::getPartDate));
mDate.forEach((k, v) -> {
//根据电脑分组
Map<String, List<Work>> yqs = v.stream().collect(Collectors.groupingBy(Work::getComputer));
req.put(k, yqs);
});
sortMap.putAll(req);
System.out.println(JSONObject.toJSONString(sortMap));
}
}
结果
{
"2020-08": {
"三星": [{
"computer": "三星",
"date": "2020-08-17 12:12:12",
"num": 1,
"partDate": "2020-08"
}]
},
"2020-06": {
"苹果": [{
"computer": "苹果",
"date": "2020-06-19 12:12:12",
"num": 2,
"partDate": "2020-06"
}]
},
"2020-05": {
"惠普": [{
"computer": "惠普",
"date": "2020-05-16 15:12:12",
"num": 4444444,
"partDate": "2020-05"
}, {
"computer": "惠普",
"date": "2020-05-16 12:12:12",
"num": 555555,
"partDate": "2020-05"
}],
"华为": [{
"computer": "华为",
"date": "2020-05-18 12:12:12",
"num": 777777,
"partDate": "2020-05"
}, {
"computer": "华为",
"date": "2020-05-16 12:12:12",
"num": 4,
"partDate": "2020-05"
}],
"小米": [{
"computer": "小米",
"date": "2020-05-18 12:12:12",
"num": 3,
"partDate": "2020-05"
}]
}
}