TreeMap按照key和value排序

时间:2025-04-04 07:54:33
//按key升序排序
Map<String, String> map = new TreeMap<>();

        ("2018-07-11", "ccccc");
        ("2018-07-26", "aaaaa");
        ("2018-06-11", "bbbbb");
        ("2018-06-28", "ddddd");
        ("2017-05-29", "ccccc");
        ("2017-04-26", "aaaaa");
        ("2017-03-31", "bbbbb");
        ("2017-02-01", "ddddd");

        Iterator iterator = ().iterator();
        while (()) {
            String k = (String) ();
            (k + "--" + (k));
        }
//输出顺序
2017-02-01--ddddd
2017-03-31--bbbbb
2017-04-26--aaaaa
2017-05-29--ccccc
2018-06-11--bbbbb
2018-06-28--ddddd
2018-07-11--ccccc
2018-07-26--aaaaa

//按key降序排序
Map<String, String> map = new TreeMap<>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return (obj1);
                    }
                }
        );

        ("2018-07-11", "ccccc");
        ("2018-07-26", "aaaaa");
        ("2018-06-11", "bbbbb");
        ("2018-06-28", "ddddd");
        ("2017-05-29", "ccccc");
        ("2017-04-26", "aaaaa");
        ("2017-03-31", "bbbbb");
        ("2017-02-01", "ddddd");

        Iterator iterator = ().iterator();
        while (()) {
            String k = (String) ();
            (k + "--" + (k));
        }
//输出结果
2018-07-26--aaaaa
2018-07-11--ccccc
2018-06-28--ddddd
2018-06-11--bbbbb
2017-05-29--ccccc
2017-04-26--aaaaa
2017-03-31--bbbbb
2017-02-01--ddddd

//按value升序排序
Map<String, String> map = new TreeMap<>(
                //如果有如下代码,key按照降序排序
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return (obj1);
                    }
                }
        );

        ("2018-07-11", "ccccc");
        ("2018-07-26", "aaaaa");
        ("2018-06-11", "bbbbb");
        ("2018-06-28", "ddddd");
        ("2017-05-29", "ccccc");
        ("2017-04-26", "aaaaa");
        ("2017-03-31", "bbbbb");
        ("2017-02-01", "ddddd");

        List<<String, String>> list = new ArrayList<<String, String>>(());
        (list,new Comparator<<String, String>>() {
            public int compare(<String, String> o1, <String, String> o2) {

                return ().compareTo(());
            }
        });

        for (<String, String> entry : list) {
            (()+"---"+());
        }
//输出结果
2018-07-26---aaaaa
2017-04-26---aaaaa
2018-06-11---bbbbb
2017-03-31---bbbbb
2018-07-11---ccccc
2017-05-29---ccccc
2018-06-28---ddddd
2017-02-01---ddddd

//按value降序排序
Map<String, String> map = new TreeMap<>(
                //如果有如下代码,key按照降序排序
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        return (obj1);
                    }
                }
        );

        ("2018-07-11", "ccccc");
        ("2018-07-26", "aaaaa");
        ("2018-06-11", "bbbbb");
        ("2018-06-28", "ddddd");
        ("2017-05-29", "ccccc");
        ("2017-04-26", "aaaaa");
        ("2017-03-31", "bbbbb");
        ("2017-02-01", "ddddd");

        List<<String, String>> list = new ArrayList<<String, String>>(());
        (list,new Comparator<<String, String>>() {
            public int compare(<String, String> o1, <String, String> o2) {

                return ().compareTo(());
            }
        });

        for (<String, String> entry : list) {
            (()+"---"+());
        }
//输出结果
2018-06-28---ddddd
2017-02-01---ddddd
2018-07-11---ccccc
2017-05-29---ccccc
2018-06-11---bbbbb
2017-03-31---bbbbb
2018-07-26---aaaaa
2017-04-26---aaaaa