[java] Map value 정렬

2021. 4. 10. 15:57Spring Boot

반응형

 

        Map<string, string=""> map = new HashMap<>();
         
        map.put("A", "29");
        map.put("C", "19");
        map.put("D", "31");
        map.put("B", "15");
         
        List<string> keySetList = new ArrayList<>(map.keySet());
        // 내림차순 //
        Collections.sort(keySetList, new Comparator<string>() {
            @Override
            public int compare(String o1, String o2) {
                return map.get(o2).compareTo(map.get(o1));
            }
        });
         
        for(String key : keySetList) {
            System.out.println(String.format("Key : %s, Value : %s", key, map.get(key)));
        }
         
        System.out.println("-------------------------------------------------------------");
         
        // 오름차순 //
        Collections.sort(keySetList, (o1, o2) -> (map.get(o1).compareTo(map.get(o2))));
         
        for(String key : keySetList) {
            System.out.println(String.format("Key : %s, Value : %s", key, map.get(key)));
        }

 

 

p-northeast-2.console.aws.amazon.com/ec2/v2/home?region=ap-northeast-2#Instances:

반응형