Map<String, Integer> map = ImmutableMap.of("0",3,"1",8,"0.29",7,"1.67",3); System.out.println("原始的map:"+map); System.out.println("根据map的key降序:"+ sortByKey(map,true)); System.out.println("根据map的key升序:"+ sortByKey(map,false)); System.out.println("根据map的value...
unsortMap.put("m", 2); unsortMap.put("f", 9); System.out.println("Original..."); System.out.println(unsortMap); // sort by keys, a,b,c..., and return a new LinkedHashMap // toMap() will returns HashMap by default, we need LinkedHashMap to keep the order. Map<String,...
1、根据key排序 Map<String,String> result = new HashMap<>(); Map<String,String> map = new HashMap<>(); map.entrySet().stream() .sorted(Map.Entry.comparingByKey()) .forEachOrdered(x->result.put(x.getKey(),x.getValue())); 1. 2. 3. 4. 5. 6. 7. 2、根据value排序 Map<Stri...
Map<String, Integer> map = ImmutableMap.of("0", 3, "1", 8, "0.29", 7, "1.67", 3); System.out.println("原始的map:" +map); System.out.println("根据map的key降序:" + sortByKey(map,true)); System.out.println("根据map的key升序:" + sortByKey(map,false)); ...
2019-04-08 23:12 −熟悉下java8的新特性对map排序操作,干货满满~... superdrew 1 12326 在map中根据value获取key 2019-12-09 10:56 −//根据map的value获取map的key private static String getKey(Map<String,String> map,String value){ String key=""; for (Map.Entry<String, S... ...
可以通过 stream 来使用 comparingByKey 进行排序。 代码语言:java 复制 List<Map.Entry<String,Integer>>collect=map.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toList()); comparingByValue 也一样,这两个都是使用内部比较器Comparable。
按照map的Key进行排序 publicstaticvoidmain(String[]args){Map<String,Integer>unsortMap=newHashMap<>();unsortMap.put("z",10);unsortMap.put("b",5);unsortMap.put("a",6);unsortMap.put("c",20);unsortMap.put("d",1);unsortMap.put("e",7);unsortMap.put("y",8);unsortMap.put...
(",");// Map<String, String> map = IntStream.range(0, keysArray.length)// .mapToObj(i -> new AbstractMap.SimpleEntry<>(keysArray[i], values.split(",")[i]))// .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));/// System.out.println("---" + map);// }...
一、少数key的情况 有一个需求:根据 menu_level,sort排序,越小的越前面。 -- 下面代码按照升序规则进行! -- Collections.sort(menuList, new Comparator<Map<String, Object>>() { @Override public int compare(Map<String, Object> o1, Map<String, Object> o2) { ...