集合排序

public static void main(String[] args){

Map setToneMoneyMap = new HashMap();

setToneMoneyMap.put("1","75");

setToneMoneyMap.put("2","2");

setToneMoneyMap.put("3","78");

setToneMoneyMap.put("4","12");

setToneMoneyMap.put("5","1212");

List> list = new ArrayList<>(setToneMoneyMap.entrySet());

        Collections.sort(list, new Comparator>() {

            public int compare(Map.Entry o1, Map.Entry o2) {

                int compare = (Double.valueOf(o1.getValue())).compareTo(Double.valueOf(o2.getValue()));

            //降序添加负号,升序不加负号

            return -compare;

            }

        });


        for (Map.Entry entry : list) {

        System.out.println("key: "+entry.getKey()+" value: "+entry.getValue());

        }

}

你可能感兴趣的:(集合排序)