Java 8 stream map to list of keys sorted by values

前端 未结 6 1267
夕颜
夕颜 2020-12-03 06:29

I have map Map countByType and I want to have a list which has sorted (min to max) keys by their corresponding values. My try is:



        
相关标签:
6条回答
  • 2020-12-03 07:01

    You can sort a map by value as below, more example here

    //Sort a Map by their Value.
    Map<Integer, String> random = new HashMap<Integer, String>();
    
    random.put(1,"z");
    random.put(6,"k");
    random.put(5,"a");
    random.put(3,"f");
    random.put(9,"c");
    
    Map<Integer, String> sortedMap =
            random.entrySet().stream()
                    .sorted(Map.Entry.comparingByValue())
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                            (e1, e2) -> e2, LinkedHashMap::new));
    System.out.println("Sorted Map: " + Arrays.toString(sortedMap.entrySet().toArray()));
    
    0 讨论(0)
  • 2020-12-03 07:01

    Here is the simple solution with StreamEx

    EntryStream.of(countByType).sortedBy(e -> e.getValue()).keys().toList();
    
    0 讨论(0)
  • 2020-12-03 07:09

    You say you want to sort by value, but you don't have that in your code. Pass a lambda (or method reference) to sorted to tell it how you want to sort.

    And you want to get the keys; use map to transform entries to keys.

    List<Type> types = countByType.entrySet().stream()
            .sorted(Comparator.comparing(Map.Entry::getValue))
            .map(Map.Entry::getKey)
            .collect(Collectors.toList());
    
    0 讨论(0)
  • 2020-12-03 07:13

    You have to sort with a custom comparator based on the value of the entry. Then select all the keys before collecting

    countByType.entrySet()
               .stream()
               .sorted((e1, e2) -> e1.getValue().compareTo(e2.getValue())) // custom Comparator
               .map(e -> e.getKey())
               .collect(Collectors.toList());
    
    0 讨论(0)
  • 2020-12-03 07:13
    Map<Integer, String> map = new HashMap<>();
    map.put(1, "B");
    map.put(2, "C");
    map.put(3, "D");
    map.put(4, "A");
    
    List<String> list = map.values()
                           .stream()
                           .sorted()
                           .collect(Collectors.toList());
    

    Output: [A, B, C, D]

    0 讨论(0)
  • 2020-12-03 07:13

    You can use this as an example of your problem

        Map<Integer, String> map = new HashMap<>();
        map.put(10, "apple");
        map.put(20, "orange");
        map.put(30, "banana");
        map.put(40, "watermelon");
        map.put(50, "dragonfruit");
    
        // split a map into 2 List
        List<Integer> resultSortedKey = new ArrayList<>();
        List<String> resultValues = map.entrySet().stream()
                //sort a Map by key and stored in resultSortedKey
                .sorted(Map.Entry.<Integer, String>comparingByKey().reversed())
                .peek(e -> resultSortedKey.add(e.getKey()))
                .map(x -> x.getValue())
                // filter banana and return it to resultValues
                .filter(x -> !"banana".equalsIgnoreCase(x))
                .collect(Collectors.toList());
    
        resultSortedKey.forEach(System.out::println);
        resultValues.forEach(System.out::println);
    
    0 讨论(0)
提交回复
热议问题