How to sort HashMap<String, String[]> by String in android
Solution 1
If you need special ordering, and the ability to have multiple objects that have an equal "key", that screams for List
, with a custom Comparator
.
1- Define a class of elements to store in your list. In your case, it's an object that has 2 fields: a "key" and an array of String. Let's call it CustomObject
(you can call it as you like)
2- Stick all your objects in the list
Like that:
list.add(new CustomObject("abc", new String[] {"a", "b", "c"});
list.add(new CustomObject("abc", new String[] {"aa", "bb", "cc"});
list.add(new CustomObject("def", new String[] {"d", "e", "f"});
3- Order your list using a custom comparator.
To order the list do
Collections.sort(list, new Comparator<CustomObject>() {
@Override
public int compare(CustomObject o1, CustomObject o2) {
return o1.getArray().compare(o2.getArray());
}
});
(your comparator needs to be a bit more sophisticated, to properly compare arrays, but you see my point).
An alternative to that is to add a natural ordering to your CustomObject
(implements Comparable
), and stick them into a TreeSet
.
Solution 2
The HashMap can not be sorted (or contain duplicate keys), it is part of how it is implemented (see the documentation).
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
So you better do as other people are suggesting and switch to a list or a different map implementation.
Comments
-
Ovidiu Birgu almost 2 years
I have this code:
HashMap<String, String[]> unsorted = new HashMap<String, String[]>(); String[] values = new String[3]; String key; //add data to hashmap key = "abc"; values[0] = "a"; values[1]="b"; values[2]="c"; unsorted.put(key, values); key = "abc"; values[0] = "aa"; values[1]="bb"; values[2]="cb"; unsorted.put(key, values); key = "def"; values[0] = "d"; values[1]="e"; values[2]="f"; unsorted.put(key, values); //sort hashmap /***********/ //output should be: { abc-[a,b,c], abc-[aa,bb,cc], def-[d,e,f] } //or { abc-[aa,bb,cc], abc-[a,b,c], def-[d,e,f] }
How can I sort it like that? Note: I tried with using TreeMap, and other examples, but they eliminate the elements where the keys are equal.
Edit: I solved my problem :) thanks to Guillaume. Here is what I used:
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class test { public static void main(String[] args) { ArrayList<CustomObject> objs = new ArrayList<CustomObject>(); objs.add(new CustomObject("abc", new String[] {"a", "b", "c"})); objs.add(new CustomObject("def", new String[] {"d", "e", "f"})); objs.add(new CustomObject("abc", new String[] {"aa", "bb", "cc"})); System.out.println(objs.isEmpty()); Collections.sort(objs, new Comparator<CustomObject>() { @Override public int compare(CustomObject o1, CustomObject o2) { int i = o1.getKey().compareTo(o2.getKey()); if(i == 0) return -1; return i; } }); for(int i=0; i<objs.size(); i++) System.out.println("key/value pair:" + objs.get(i).getKey() + " - " + objs.get(i).getValues()[0]); } }
And the CustomObject:
public class CustomObject { private String key; private String[] values; public CustomObject(String key, String[] values) { this.key = key; this.values = values; } public String getKey() { return key; } public String[] getValues() { return values; } }
-
Ovidiu Birgu over 12 yearsthanks, I'll try the first method, but do I have to define every function for the items list?? (public void add(int index, CustomObject element), public CustomObject get(int index), etc)
-
Guillaume over 12 yearsNo, no, use an ArrayList :) I said List because it's the interface, use any of its implementations (ArrayList or LinkedList)
-
Guillaume over 12 yearsGlad it helped :) But really, you should find a proper name instead of CustomObject, something that describes what your objects are