Skip to content

Instantly share code, notes, and snippets.

@umbertogriffo
Last active May 16, 2017 13:46
Show Gist options
  • Save umbertogriffo/22991c796c0d60cee89fa25a1f501529 to your computer and use it in GitHub Desktop.
Save umbertogriffo/22991c796c0d60cee89fa25a1f501529 to your computer and use it in GitHub Desktop.
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
/**
* Created by Umberto on 16/05/2017.
*/
public class HashMapUtils {
/**
* Convert an HashMap to a well-formatted String
* @param map
* @return A String containing the HashMap
*/
public static <K, V> String toString(Map<K, V> map) {
StringBuilder sb = new StringBuilder();
Iterator<Entry<K, V>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Entry<K, V> entry = iter.next();
sb.append(entry.getKey());
sb.append('=').append('"');
sb.append(entry.getValue());
sb.append('"');
if (iter.hasNext()) {
sb.append(',').append('\n');
}
}
return sb.toString();
}
/**
* Sort By Key
*
* @param map
* @param <K>
* @param <V>
* @return a LinkedHashMap containing the sorted HashMap
*/
public static <K extends Comparable<? super K>, V> Map<K, V> sortByKey(Map<K, V> map, boolean ascending) {
return map.entrySet()
.stream()
// Not Generic .sorted(Map.Entry.<String, Integer>comparingByKey().reversed())
.sorted((ascending) ? Map.Entry.comparingByKey() : Map.Entry.comparingByKey(Collections.reverseOrder()))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1,
LinkedHashMap::new
));
}
/**
* Sort By Value
*
* @param map
* @param <K>
* @param <V>
* @return a LinkedHashMap containing the sorted HashMap
*/
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map, boolean ascending) {
return map.entrySet()
.stream()
// Not Generic .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
.sorted((ascending) ? Map.Entry.comparingByValue() : Map.Entry.comparingByValue(Collections.reverseOrder()))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1,
LinkedHashMap::new
));
}
/**
* Print a Generic Map
*
* @param map
* @param <K>
* @param <V>
*/
public static <K, V> void printMap(Map<K, V> map) {
for (Map.Entry<K, V> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
}
}
public static void main(String args[]) {
Map<String, Integer> unsortMap = new HashMap<String, Integer>();
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("n", 99);
unsortMap.put("j", 50);
unsortMap.put("m", 2);
unsortMap.put("f", 9);
System.out.println("Unsort Map......");
printMap(unsortMap);
Map<String, Integer> sortedMapByKey = HashMapUtils.sortByKey(unsortMap, true);
System.out.println("Sorted Map By Key ......");
printMap(sortedMapByKey);
Map<String, Integer> sortedMapByValue = HashMapUtils.sortByValue(unsortMap, true);
System.out.println("Sorted Map By Value ......");
printMap(sortedMapByValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment