Sort a Map≪Key, Value≫ by Values

Sorting HashMap by values

Assuming Java, you could sort hashmap just like this:

public LinkedHashMap<Integer, String> sortHashMapByValues(
HashMap<Integer, String> passedMap) {
List<Integer> mapKeys = new ArrayList<>(passedMap.keySet());
List<String> mapValues = new ArrayList<>(passedMap.values());
Collections.sort(mapValues);
Collections.sort(mapKeys);

LinkedHashMap<Integer, String> sortedMap =
new LinkedHashMap<>();

Iterator<String> valueIt = mapValues.iterator();
while (valueIt.hasNext()) {
String val = valueIt.next();
Iterator<Integer> keyIt = mapKeys.iterator();

while (keyIt.hasNext()) {
Integer key = keyIt.next();
String comp1 = passedMap.get(key);
String comp2 = val;

if (comp1.equals(comp2)) {
keyIt.remove();
sortedMap.put(key, val);
break;
}
}
}
return sortedMap;
}

Just a kick-off example. This way is more useful as it sorts the HashMap and keeps the duplicate values as well.

how to sort a map by key when the values are the same?

You could check in your Comparator if the values are the same and if so compare the keys. Here is your adapted method:

public static <K extends Comparable<? super K>, V extends Comparable<? super V>> Map<K, V> newSortMapByValue(Map<K, V> map) {
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
@Override
public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
// Check if values are the same
if (e1.getValue().equals(e2.getValue()))
// Compare e1 to e2, because A should be first element
return e1.getKey().compareTo(e2.getKey());
else
// Compare e2 to e1, because largest number should be first
return e2.getValue().compareTo(e1.getValue());
}
});

Map<K, V> result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}

Example main:

    public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("AA",10);
map.put("CC",5);
map.put("BB",5);
map.put("DD",15);
map.put("ZZ",15);

System.out.println(map);
Map sortedMap = newSortMapByValue(map);
System.out.println(sortedMap);
}

Output:

{AA=10, CC=5, BB=5, DD=15, ZZ=15}
{DD=15, ZZ=15, AA=10, BB=5, CC=5}

Sort a map on key and value

import java.util.SortedSet;
import java.util.TreeSet;

public class SortMapOnKeyAndValue {

public static void main(String[] args) {
SortedSet<KeyValuePair> sortedSet = new TreeSet<KeyValuePair>();
sortedSet.add(new KeyValuePair(1, 2));
sortedSet.add(new KeyValuePair(2, 2));
sortedSet.add(new KeyValuePair(1, 3));
sortedSet.add(new KeyValuePair(2, 1));

for (KeyValuePair keyValuePair : sortedSet) {
System.out.println(keyValuePair.key+","+keyValuePair.value);
}
}
}
class KeyValuePair implements Comparable<KeyValuePair>{
int key, value;

public KeyValuePair(int key, int value) {
super();
this.key = key;
this.value = value;
}

public int compareTo(KeyValuePair o) {
return key==o.key?value-o.value:key-o.key;
}
}


Related Topics



Leave a reply



Submit