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 … Read more

“Cannot create generic array of ..” – how to create an Array of Map?

Because of how generics in Java work, you cannot directly create an array of a generic type (such as Map<String, Object>[]). Instead, you create an array of the raw type (Map[]) and cast it to Map<String, Object>[]. This will cause an unavoidable (but suppressible) compiler warning. This should work for what you need: Map<String, Object>[] … Read more