对地图进行排序Map <string,list <string =“” >>

时间:2019-02-20 08:49:06

标签: java java-7

大家好,我正在尝试对地图进行排序> 映射键集包含顺序1 2 3 4 ext ...

我正在检索数据的文件 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;
    }

我想返回按我尝试过的键排序的地图     //地图treeMap = new TreeMap((Comparator)filterMap);

谢谢

2 个答案:

答案 0 :(得分:0)

您必须使用比较器才能使其正常工作!像这样:

driver.findElement(By.xpath("//c- 
    wiz[@class='boqChromeogbviewView_']")).sendKeys(Keys.ESCAPE);

答案 1 :(得分:0)

请考虑以下代码。使用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));

输出根据键进行排序:

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