맵 에서 value 정렬 하기

public class TestMap {

    public static void main(String[] args) {
        HashMap<String,Integer> map = new HashMap<String,Integer>();
        CompareValue cvm = new CompareValue(map);
        TreeMap Treemap= new TreeMap(cvm);
        map.put("b",1);
        map.put("a",30);
        map.put("c", 20);
        Treemap.putAll(map);
        System.out.println(Treemap);
    }

}
class CompareValue implements Comparator<String>{
    private HashMap<String,Integer> maps;
    public CompareValue(HashMap<String,Integer> maps){
        this.maps=maps;
    }


    @Override
    public int compare(String o1, String o2) {
        if(maps.get(o1)>maps.get(o2)){
            return -1;
        }else  if (maps.get(o1)<maps.get(o2)){
            return 1;
        }
        return 0;
    }
}

좋은 웹페이지 즐겨찾기