ArrayList <HashMap <String, String >> in alphabetical order

How can I arrange an ArrayList from HashMaps using a Hashmap key? I have a list of names and numbers (stored as a string) inside an ArrayList.

+4
source share
4 answers

I have a list of names and numbers (stored as a string) inside an arralist.

I think the real problem here (i.e. why it is difficult for you to describe and implement this) is that you are using the wrong data structure. If the objects in ArrayList are for lists, you should not represent them using HashMaps. HashMaps are not lists of pairs, since they do not preserve the order of pairs. And by the sounds of this, you need to keep this order in order to give you a criterion for ordering data structures of a โ€œlist of pairsโ€ in a larger ArrayList array.

If my understanding is correct, then it will be difficult for you to order an ArrayList ... until you fix the problem of how internal "lists" will be presented. It is unclear what this solution should be:

  • Perhaps it should be ArrayList<Pair<String,String>> , where Pair is a simple tuple. This will allow you to arrange lists based on the insertion order of their items.

  • Maybe it should be LinkedHashMap<String,String> . It also allows you to organize lists based on the insertion order of their elements, while maintaining a quick search by name ... if that matters.

  • Perhaps it should be TreeMap<String, String> . This allows you to order keys (names in your case).

  • Perhaps it should be TreeSet<Pair<String, String>> . This allows you to order either keys (names), or values, or a combination of both.

+12
source

I think the crawler wants to know how to sort an ArrayList from a HashMap (String, String), because Android needs this structure to add menu items to the 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 cases, HashMaps has the same key, so you can use my code. (You can change the "Name" to your key.

+7
source

Use Collections.sort(List, Comparator) and put the 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 other editors have pointed out, you're probably abusing HashMap. My answer above is literally a solution to the question you asked, namely: "How can I sort the list of cards?" I will leave it here for prosperity.

I suspect that your ideal solution would be a normal class with a natural order ( Comparable implementation) to store all the specific information about a person, and then add its objects to a SortedSet , such as a TreeSet .

+4
source

We will 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; } } 

Conclusion:

 $ java SortMap Before: [{Zen=0}, {April=0.5}, {Oscar=1}, {Luca=2}] After: [{April=0.5}, {Luca=2}, {Oscar=1}, {Zen=0}] 

Is this what you are looking for?

EDIT (read just in case what exactly you were looking for)

This will be a much better way using the right data structure to work!

 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} 
+1
source

Source: https://habr.com/ru/post/1312612/


All Articles