简体   繁体   English

如何对 Map 类型的地图进行排序<String, List<String> &gt; 在 Java 中

[英]How to sort a map of type Map<String, List<String>> in Java

Issue问题

I'm trying to sort a map Map> which the map key set contain the order 1 2 3 4 ext ...我正在尝试对地图 Map> 进行排序,其中地图键集包含顺序 1 2 3 4 ext ...

Code代码

The file which I'm retrieving data filter.properties 1=gwtCacheControlFilter:com.palmyra.arch.presentation.port.server.GWTCacheControlFilter:true:/*:null:presentation我正在检索数据的文件filter.properties 1=gwtCacheControlFilter:com.palmyra.ar​​ch.presentation.port.server.GWTCacheControlFilter:true::/*:null:presentation

    public Map<String, List<String>> initiateMapOfFilters() throws IOException {

        Map<String, List<String>> filterMap = new HashMap<>();

        Properties properties = new Properties();
        properties.load(FilterFileStream);

        for (Entry<Object, Object> filterFromFile : properties.entrySet()) {

            String filterValue = filterFromFile.getValue().toString();
            String[] split = filterValue.split(":");

            ArrayList<String> list = new ArrayList<>();
            for (String s : split) {
                list.add(s);
            }


            //-------sort the list with order
            filterMap.put(split[filterNameIndex], list);        

        }
//      Map<String, String> treeMap = new TreeMap<String, String>((Comparator<? super String>) filterMap); comment
        return filterMap;
    }

What I've tried我试过的

I want to return a map ordered by the key I tried:我想返回按我试过的键排序的地图:

Map<String, String> treeMap = new TreeMap<String, String>((Comparator<? super String>) filterMap);

Thank you for any suggestion.谢谢你的任何建议。

You have to use a comparator to make it work!您必须使用比较器才能使其工作! Like this :像这样 :

Map<Integer,String> unsortedMap = new Hashmap<Integer,String>();
Map<Integer,String> treeMap = new TreeMap<Integer,String>(
    new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o2.compareTo(o1);//sort in descending order
        }
        });

Consider the below code.考虑下面的代码。 With java 8 you can use the Comparator.comparing method and get it done quite quickly.使用 java 8,您可以使用 Comparator.comparing 方法并很快完成。

        TreeMap<String, List<String>> treeMap = new TreeMap<>(Comparator.comparing(t -> Integer.valueOf(t)));

        treeMap.put("5", Arrays.asList(new String[] {"data1", "data2", "data3"}));
        treeMap.put("3", Arrays.asList(new String[] {"data4", "data5", "data6"}));
        treeMap.put("1", Arrays.asList(new String[] {"data7", "data8", "data9"}));
        treeMap.put("4", Arrays.asList(new String[] {"data10", "data11", "data12"}));

        treeMap.
                forEach((k,v) -> System.out.println(k + "=="+v));

Output is sorted on basis of keys:输出根据键排序:

1==[data7, data8, data9]
3==[data4, data5, data6]
4==[data10, data11, data12]
5==[data1, data2, data3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM