I've a SortedMap
which have items like:
- 1=abc
- 2=xyz
- 3=abc
Values can be duplicate.
I want to display the value-set on screen in the sorted manner. i.e.
myListMap
- abc
- abc
- xyz
To sort the Map
, I'm using comparator
:
public class SortMapByValueComparator implements Comparator<String> {
Map<String, String> mInputMap;
/**
* Constructor.
*
* @param inputMap
*/
public SortMapByValueComparator(Map<String, String> inputMap) {
mInputMap = inputMap;
}
@Override
public int compare(String lhs, String rhs) {
String string1 = mInputMap.get(lhs);
String string2 = mInputMap.get(rhs);
return string1.compareToIgnoreCase(string2);
}
}
And then passing the Map to this comparator like:
SortMapByValueComparator sortMapByValueComparator = new SortMapByValueComparator(myListMap);
SortedMap<String, String> sortedMapByValue = new TreeMap<String, String>(sortMapByValueComparator);
sortedMapByValue.putAll(myListMap);
Now, issue is when I call SortMapByValueComparator
, it removes duplicate values. How can I avoid it?
PS - I want to use Map only.