java map排序

public void statisticsA(){
        List<Moves> file = this.file();
        Map<String,Integer> map = new HashMap<>();
        int init=1;
        for (Moves moves1 : file) {
            if("".equals(moves1.getDate())||moves1.getDate()==null){
                continue;
            }
            if(map.containsKey(moves1.getDate())){
                Integer count = map.get(moves1.getDate());
                map.put(moves1.getDate(),++count);
            }else{
                map.put(moves1.getDate(),init);
            }
        }
        ArrayList<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
        Collections.sort(entries,new Comparator<Map.Entry<String, Integer>>(){
            @Override
            public int compare(Map.Entry<String, Integer> v1, Map.Entry<String, Integer> v2) {
                return Integer.valueOf(v1.getKey()) - Integer.valueOf(v2.getKey());
            }
        });
        Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
        while(iterator.hasNext()){
            Map.Entry<String, Integer> next = iterator.next();
            String key = next.getKey();
            Integer value = next.getValue();
            System.out.println("[ "+key+" ]年: 发行( "+value+" )部电影");
        }
    }

 

posted on 2020-11-26 20:04  Difcipo  阅读(75)  评论(0编辑  收藏  举报

导航