ArrayList<HashMap<String,String>> in alphabetical order
How can I order an ArrayList of HashMaps by the key of the Hashmap? I've a list of names and numbers (saved as string开发者_高级运维) inside an ArrayList.
I've a list of names and numbers(saved as string) inside an arralist.
I think the real problem here (i.e. why you are having difficulty describing and implementing this) is that you are using the wrong data structure. If the objects in the ArrayList are intended to be lists, then you should not represent them using HashMaps. HashMaps are not lists of pairs since they do not preserve the order of the pairs. And by the sounds of it, you need that order to be preserved to give you the criterion for ordering the "list of pair" data structures in the larger ArrayList.
If my understanding is correct, then you will have difficulty ordering the ArrayList ... until you fix the problem of how the inner "lists" are represented. It is not clear what that fix should be:
Maybe it needs to be
ArrayList<Pair<String,String>>
wherePair
is a simple tuple. This will allow you to order the lists based on the insertion order of their elements.Maybe it needs to be
LinkedHashMap<String,String>
. This also allows you to order the lists based on the insertion order of their elements, while still supporting fast lookup by name ... if that is important.Maybe it needs to be
TreeMap<String, String>
. This allows you to order on the keys (names in your case).Maybe it needs to be
TreeSet<Pair<String, String>>
. This allows you to order on the either the keys (names) or the values, or some combination thereof.
I think the asker wants to know how to sort ArrayList of HashMap(String,String) because Android needs this structure for adding menuItems to ListView.
You can use:
//Sort the filenames by songTitle
Collections.sort(Arraylist,new Comparator<HashMap<String,String>>(){
public int compare(HashMap<String,String> mapping1,HashMap<String,String> mapping2){
return mapping1.get("Name").compareTo(mapping2.get("Name"));
}
In most case, HashMaps have same key , so you can use my code.( You can change the "Name" to your key.
Use Collections.sort(List, Comparator)
and supply a custom Comparator
:
Collections.sort(list, new Comparator<Map<String,String>>() {
@Override
public int compare(Map<String,String> o1, Map<String,String> o2) {
return /* your comparison here */;
}
});
Edit:
As the other editors pointed out, you're likely misusing HashMap. My answer above is literally the solution to the question you were asking, which was "how can I sort a List of Maps?" I'll leave it here for prosperity.
I suspect your ideal solution would be a custom class with a natural order (implementing Comparable
) to hold all of a particular person's information, then adding its objects to a SortedSet
such as TreeSet
.
Let's see.
This program:
import java.util.*;
public class SortMap {
public static void main( String [] args ) {
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
list.add( getMap("Zen", "0"));
list.add( getMap("April", "0.5"));
list.add( getMap("Oscar", "1") );
list.add( getMap("Luca", "2") );
System.out.println( "Before: "+ list );
Collections.sort( list, new Comparator<Map<String,String>>(){
public int compare( Map<String,String> one, Map<String,String> two ) {
return one.keySet().iterator().next().compareTo( two.keySet().iterator().next() );
}
});
System.out.println( "After: "+ list );
}
private static Map<String,String> getMap(String key, String value ) {
Map<String,String> map = new HashMap<String,String>();
map.put(key,value);
return map;
}
}
Output:
$ java SortMap
Before: [{Zen=0}, {April=0.5}, {Oscar=1}, {Luca=2}]
After: [{April=0.5}, {Luca=2}, {Oscar=1}, {Zen=0}]
Is that what you're looking?
EDIT ( read just in case that was what you were looking for )
This would be a way much better implementation by using the correct data structure for the job!
class SortMapBetter {
public static void main( String [] args ) {
Map<String,String> sortedMap = new TreeMap<String,String>();
sortedMap.put("Zen", "0");
sortedMap.put("April", "0.5");
sortedMap.put("Oscar", "1");
sortedMap.put("Luca", "2");
System.out.println( sortedMap );
}
}
$ java SortMapBetter
{April=0.5, Luca=2, Oscar=1, Zen=0}
精彩评论