In Java, I need to group hash map keys based on their values

I need to find the topN keys.

I have an input as a HashMap in the form as (key: value):

Banana : 13  
Apple: 12  
Mango : 32  
Orange : 12  
Grape : 18  
Pear : 12  
Peach : 18  

I created a related HapMap that sorts based on values:

private static <K extends Comparable, V extends Comparable> Map<K, V> sortByValues(Map<K, V> map) {
    List<Map.Entry<K, V>> entries = new LinkedList<Map.Entry<K, V>>(map.entrySet());

    Collections.sort(entries, new Comparator<Map.Entry<K, V>>() {

        @Override
        public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
            return o2.getValue().compareTo(o1.getValue());
        }
    });
    Map<K, V> sortedMap = new LinkedHashMap<K, V>();
    for (Map.Entry<K, V> entry : entries) {
        sortedMap.put(entry.getKey(), entry.getValue());
    }
    return sortedMap;
}

this gave me the conclusion:

Mango : 32  
Grape : 18  
Peach : 18  
Banana : 13  
Apple: 12  
Orange : 12  
Pear : 12  

Now, if I want to use the fruits of Top-4, how should I go if I want the result to be as follows:

Mango :32  
Grape, Peach : 18  
Banana :13  
Apple, Orange, Pear: 12  

I tried iterating through a sorted hash map and comparing the values ​​of the following elements by doing

int sizeOfMap = myValueSortedMap.size();
ArrayList<String> keyArr = new ArrayList<String>();
int cnt=0,keyVal=0;

while(cnt<(sizeOfMap-1)){

    if(myValueSortedMap.values().toArray()[cnt] == myValueSortedMap.values().toArray()[cnt+1]){

        keyArr.add((String) myValueSortedMap.keySet().toArray()[cnt]+ " , " +(String) myValueSortedMap.keySet().toArray()[cnt+1]);
    }
    else{
        keyArr.add((String) myValueSortedMap.keySet().toArray()[cnt]);
        keyVal = (int) myValueSortedMap.values().toArray()[cnt];
    }
    cnt++;
}

but it does not always work.

I can’t think about it. Can someone please give me an edge?

+4
source share
5 answers

- , , 4 .

map.entrySet()
   .stream()
   .collect(Collectors.groupingBy(Map.Entry::getValue))
   // the map now contains integers mapped to a list of map entries
   .entrySet()
   .stream()
   // sort the stream by descending numeric value
   .sorted((o1, o2) -> o2.getKey().compareTo(o1.getKey()))
   // use the first four elements of the stream
   .limit(4)
   .forEach(entry -> System.out.println(entry.getKey() + " " + entry.getValue().stream().map(Map.Entry::getKey).collect(Collectors.joining(", "))));

32 Mango
18 Grape, Peach
13 Banana
12 Apple, Pear, Orange
+7

, : Map<String, Integer> Map<Integer, List<String>>.

4 - ( )

public static <K extends Comparable, V extends Comparable> Map<V, List<K>> reverseMap(Map<K, V> map) {
    Map<V, List<K>> result = new HashMap<>();
    map.entrySet().stream().forEach((entry) -> {
        List<K> lst = result.get(entry.getValue());
        if (lst == null) {
            lst = new ArrayList<>();
            result.put(entry.getValue(), lst);
        }
        lst.add(entry.getKey());
    });
    return result;
}

Map<String, Integer> map = new HashMap<>();
map.put("Banana", 13);
map.put("Apple", 12);
map.put("Mango", 32);
map.put("Orange", 12);
map.put("Grape", 18);
map.put("Pear", 12);
map.put("Peach", 18); 

Map<Integer, List<String>> flip = reverseMap(map);

flip.entrySet().stream()
            .sorted(Map.Entry.<Integer, List<String>>comparingByKey().reversed())
            .limit(4)
            .forEach(e -> System.out.println(String.join(", ", e.getValue()) + " : " + e.getKey()));

Mango : 32
Grape, Peach : 18
Banana : 13
Apple, Pear, Orange : 12
+2

HashMap k.

private HashMap<String, Integer> select_k_element(HashMap sortedMap,int k){
    HashMap<String, Integer> res=new HashMap();
    int i=0;
    for (Map.Entry<String, Integer> e : sortedMap.entrySet()) {
        String key=e.getKey();
        int value=e.getValue();
        res.put(key,value);
        i++;
        if(i>=k)
            break;
    }
    return res;
}
+1

Map - ( ). :

private static Map<String, Integer> fruits = new HashMap<>();

static {
    fruits.put("Banana",13);  
    fruits.put("Apple", 12);
    fruits.put("Mango",32);
    fruits.put("Orange",12); 
    fruits.put("Grape",18);
    fruits.put("Pear",12);
    fruits.put("Peach",18); 
}

public static void main(String[] args) {
    TreeMap<Integer, String> freq = new TreeMap<>();

    // Populate the new Map
    fruits.entrySet().forEach(e -> {
        Integer key = e.getValue();
        if(freq.containsKey(key)) { // Update entry if key is already present
            String curVal = freq.get(key);
            String newVal = e.getKey();

            freq.put(key, curVal + ", " + newVal);
        } else { // Add an entry if key is not present
            freq.put(key, e.getKey());
        }
    });

    // Print the new Map
    freq.descendingMap().entrySet().forEach(e -> {
        System.out.println(e.getValue() + " : " + e.getKey());
    });
}

TreeMap, descendingMap() .

:

Mango : 32
Grape,Peach : 18
Banana : 13
Apple,Pear,Orange : 12
+1

SortedMap "", Compareable.

, . ( entrySet, keySet ). , . ( SortedSet.)

+1

All Articles