I have a Map that has strings for both keys and values.
Data is like following:
"question1", "1"
"question9", "1"
"question2", "4"
"question5", "2"
I want to sort the map based on its keys. So, in the end, I will have question1, question2, question3
....and so on.
Eventually, I am trying to get two strings out of this Map.
Right now I have the following:
Iterator it = paramMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry) it.next(); questionAnswers += pairs.getKey() + ","; }
This gets me the questions in a string but they are not in order.
For example, let's take a look at how we can sort String keys by length in a HashMap , using the length of the Strings, and a custom comparator: Map<String, Integer> sortedMap = new TreeMap<>(new Comparator<String>() { @Override public int compare(String o1, String o2) { int lengthDifference = o1. length() - o2.
Example: Sort a map by values Inside the method, we first created a list named capitalList from the map capitals . We then use the sort() method of Collections to sort elements of the list. The sort() method takes two parameters: list to be sorted and a comparator. In our case, the comparator is a lambda expression.
No two mapped values can have equal key values. By default, a Map in C++ is sorted in increasing order based on its key.
Use a TreeMap
. This is precisely what it's for.
If this map is passed to you and you cannot determine the type, then you can do the following:
SortedSet<String> keys = new TreeSet<>(map.keySet()); for (String key : keys) { String value = map.get(key); // do something }
This will iterate across the map in natural order of the keys.
Technically, you can use anything that implements SortedMap
, but except for rare cases this amounts to TreeMap
, just as using a Map
implementation typically amounts to HashMap
.
For cases where your keys are a complex type that doesn't implement Comparable or you don't want to use the natural order then TreeMap
and TreeSet
have additional constructors that let you pass in a Comparator
:
// placed inline for the demonstration, but doesn't have to be a lambda expression Comparator<Foo> comparator = (Foo o1, Foo o2) -> { ... } SortedSet<Foo> keys = new TreeSet<>(comparator); keys.addAll(map.keySet());
Remember when using a TreeMap
or TreeSet
that it will have different performance characteristics than HashMap
or HashSet
. Roughly speaking operations that find or insert an element will go from O(1) to O(Log(N)).
In a HashMap
, moving from 1000 items to 10,000 doesn't really affect your time to lookup an element, but for a TreeMap
the lookup time will be about 3 times slower (assuming Log2). Moving from 1000 to 100,000 will be about 6 times slower for every element lookup.
Assuming TreeMap is not good for you (and assuming you can't use generics):
List sortedKeys=new ArrayList(yourMap.keySet()); Collections.sort(sortedKeys); // Do what you need with sortedKeys.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With