Key Based Hash Card Sort

I have the following hashmap in java:

{B046 = 0.0, A061 = 3.0, A071 = 0.0, B085 = 0.0, B075 = 3.0, B076 = 9.0, B086 = 3.0, B095 = 0.0, B096 = 0.0, A052 = 0.0, B066 = 0.0, B056 = 9.0, B065 = 0.0, B055 = 9.0}

How do I sort the hash map so that the alphabet is taken into account, followed by numerical digits?

The hashmap result should look like this:

{A052 = 0.0, A061 = 3.0, A071 = 0.0, B046 = 0.0, B055 = 9.0, B056 = 9.0, B065 = 0.0, B066 = 0.0, B075 = 3.0, B076 = 9.0, B085 = 0.0, b086 = 3.0, B095 = 0.0, B096 = 0.0}

Appreciate the help!

+67
java hashmap
Oct 22 '11 at 16:14
source share
9 answers

Use sorted TreeMap :

 Map<String, Float> map = new TreeMap<>(yourMap); 

It automatically places entries sorted by key. I think the natural String order would be good in your case.

Please note that HashMap does not keep order due to search optimization.

+201
Oct 22 '11 at 16:16
source share

Use TreeMap with a custom comparator.

 class MyComparator implements Comparator<String> { public int compare(String o1,String o2) { // Your logic for comparing the key strings } } TreeMap<String, Float> tm = new TreeMap<String , Float>(new MyComparator()); 

As you add new items, they will be automatically sorted.

In your case, you may not even need to implement a comparator, since ordering the lines may be sufficient. But if you want to implement special cases, for example, letters in lower case appear before upper case or handle numbers in a certain way, use a comparator.

+23
Oct 22 '11 at 16:18
source share

TreeMap is your best bet for this kind of sorting (Natural). TreeMap naturally sorted according to keys.

HashMap does not preserve the insertion order and does not sort the map. LinkedHashMap keeps the insertion order, but does not automatically sort the map. Only the TreeMap in the Map interface sorts the map according to the natural order (first digits, first uppercase letters, last lowercase alphabet).

+10
Oct 22 '11 at 17:03
source share

Use TreeMap , although having a map "seems" a little foggy - you can also just sort the keys based on your criteria and iterate over the map, extracting each object.

+5
Oct 22 '11 at 16:16
source share

Just use TreeMap . It implements the SortedMap interface and, thus, automatically sorts the keys contained in it. Your keys can simply be sorted alphabetically to get the desired result, so you don’t even need to provide a comparator.

HashMaps are never sorted. The only thing you do with HashMap is to get all the keys and save them in a sorted set or in a list and sort the list.

+3
Oct 22 '11 at 16:18
source share

Using TreeMap , you can sort the map.

 Map<String, String> map = new HashMap<String, String>(); Map<String, String> treeMap = new TreeMap<String, String>(map); //show hashmap after the sort for (String str : treeMap.keySet()) { System.out.println(str); } 
+3
Jan 16 '16 at 9:41
source share

You can use TreeMap , which stores the values ​​in sorted form.

 Map <String, String> map = new TreeMap <String, String>(); 
+3
Aug 07 '17 at 4:02 on
source share

TreeMap will automatically sort in ascending order. If you want to sort in descending order, use the following code:

Copy the code below into your class and outside the main execute method:

 static class DescOrder implements Comparator<String> { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } } 

Then by your logic

 TreeMap<String, String> map = new TreeMap<String, String>(new DescOrder()); map.put("A", "test1"); map.put("C", "test3"); map.put("E", "test5"); map.put("B", "test2"); map.put("D", "test4"); 
0
Dec 20 '18 at 12:36
source share
0
Jan 28 '19 at 16:54
source share



All Articles