如何将一个集合里的对象进行计算再排序
1.原来获取到的数据格式(数据格式示例)
[{"id": 1, "userName": "像我这样的人"},
{"id": 2, "userName": "像我这样的人"},
{"id": 3, "userName": "牧马城市"},
{"id": 4, "userName": "借"},
{"id": 5, "userName": "夜曲"},
{"id": 6, "userName": "如果当时"},
{"id": 7, "userName": "夜曲"},
{"id": 8, "userName": "消愁"},
{"id": 9, "userName": "像我这样的人"},
{"id": 10, "userName": "夜曲"},
....]
2.将要处理成的数据格式 (处理成想要的数据格式示例)
{
"像我这样的人":3,
"牧马城市":1,
"借":1,
"夜曲":3,
"如果当时":1,
"消愁":1
}
3.开始上代码
/**
* 歌曲播放次数的排序
* @param request
* @return
*/
@RequestMapping("/selectAll")
public Object selectAll(HttpServletRequest request) {
// 这里是我从书库获取数据的代码,得到的数据格式就是处理前的示例数据格式
List<RecordSongCount> recordSongCounts = recordSongCountService.selectAll();
// 将获取到的数据进行计算并且排序(逆序)
Map<String, Integer> sortMap = new HashMap<>(); // 先创建一个Map集合
// 遍历recordSongCounts集合的数据
for (int i = 0; i < recordSongCounts.size(); i++) {
/**
* recordSongCounts.get(i).getSongName()即为获取集合对象里的歌名
* sortMap.containsKey() 方法用来判断map集合里面是否已经有了改key
*/
if (sortMap.containsKey(recordSongCounts.get(i).getSongName())) {
/**
* 如果有了该key
* recordSongCounts.get(i).getSongName() 即为key
* sortMap.get(recordSongCounts.get(i).getSongName()) + 1 获取该key的value + 1
*/
sortMap.put(recordSongCounts.get(i).getSongName(), sortMap.get(recordSongCounts.get(i).getSongName()) + 1);
}else {
// 如果不存在即直接取名字为key, value直接赋值1
sortMap.put(recordSongCounts.get(i).getSongName(), 1);
}
}
Set<Map.Entry<String, Integer>> sortSet = sortMap.entrySet();
final boolean blog = false; // 这个为辅助变量
List<Map.Entry<String, Integer>> sortSongCount = new ArrayList<>(sortMap.entrySet());
Collections.sort(sortSongCount, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
if (o2.getValue() - o1.getValue() != 0) {
return o2.getValue() - o1.getValue();
}else {
if (blog) {
return o1.getKey().compareTo(o2.getKey());
}else {
return -(o1.getKey().compareTo(o2.getKey()));
}
}
}
});
System.out.println(sortSongCount);
// 只取前8
Integer sort = 1;
Map<String, Integer> linkMap = new LinkedHashMap<>();
for (Map.Entry<String, Integer> newEntry : sortSongCount) {
if (sort <= 8) {
linkMap.put(newEntry.getKey(), newEntry.getValue());
++sort;
}
}
return linkMap;
}
打印结果:(PS:处理之前的数据格式)
[RecordSongCount{id=1, songName='周杰伦-烟花易冷'}, RecordSongCount{id=2, songName='周杰伦-烟花易冷'}, RecordSongCount{id=3, songName='周杰伦-烟花易冷'},
RecordSongCount{id=4, songName='周杰伦-红尘客栈'}, RecordSongCount{id=5, songName='周杰伦-红尘客栈'},
RecordSongCount{id=6, songName='周杰伦-稻香'}, RecordSongCount{id=7, songName='周杰伦-稻香'}, RecordSongCount{id=8, songName='周杰伦-稻香'}, RecordSongCount{id=9, songName='毛不易-平凡的一天'}, RecordSongCount{id=10, songName='毛不易-一程山路'},
RecordSongCount{id=11, songName='许嵩-雅俗共赏'}, RecordSongCount{id=12, songName='许嵩-雅俗共赏'},
RecordSongCount{id=13, songName='胡夏-无人知晓的梦'}, RecordSongCount{id=14, songName='邓丽君-望春风'}, RecordSongCount{id=15, songName='毛不易-一程山路'},
RecordSongCount{id=16, songName='毛不易-一程山路'}, RecordSongCount{id=17, songName='毛不易-一程山路'}, RecordSongCount{id=18, songName='毛不易-一程山路'},
RecordSongCount{id=19, songName='周杰伦-烟花易冷'}, RecordSongCount{id=20, songName='周杰伦-稻香'},
RecordSongCount{id=21, songName='周杰伦-稻香'}, RecordSongCount{id=22, songName='毛不易-一程山路'}, RecordSongCount{id=23, songName='毛不易-一程山路'},
....}]
打印结果:(PS:处理完之后的数据格式,取得前八名)
{毛不易-一程山路=42, 许嵩/孙涛-书香年华=25,
毛不易-等=25, 周杰伦-烟花易冷=23,
毛不易-东北民谣=18, 毛不易-平凡的一天=17,
许嵩-惊鸿一面=12, 周杰伦-稻香=10}