如何将一个集合里的对象进行计算再排序

如何将一个集合里的对象进行计算再排序


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}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值