Java hashmap not ordered

How to Sort HashMap in Java based on Keys and Values

HashMap is not meant to keep entries in sorted order, but if you have to sort HashMap based upon keys or values, you can do that in Java. Sorting HashMap on keys is quite easy, all you need to do is to create a TreeMap by copying entries from HashMap. TreeMap is an implementation of SortedMap and keeps keys in their natural order or a custom order specified by Comparator provided while creating TreeMap. This means you can process entries of HashMap in sorted order but you cannot pass a HashMap containing mappings in a specific order, this is just not possible because HashMap doesn’t guarantee any order.

On other hand, sorting HashMap by values is rather complex because there is no direct method to support that operation. You need to write code for that. In order to sort HashMap by values you can first create a Comparator, which can compare two entries based on values.

Then get the Set of entries from Map, convert Set to List, and use Collections.sort(List) method to sort your list of entries by values by passing your customized value comparator. This is similar to how you sort an ArrayList in Java.

Half of the job is done by now. Now create a new LinkedHashMap and add sorted entries into that. Since LinkedHashMap guarantees insertion order of mappings, you will finally have a Map where contents are sorted by values.

Steps to sort HashMap by values

  1. Get all entries by calling entrySet() method of Map
  2. Create a custom Comparator to sort entries based upon values
  3. Convert entry set to list
  4. Sort entry list by using Collections.sort() method by passing your value comparator
  5. Create a LinkedHashMap by adding entries in sorted order.

Steps to sort HashMap by keys

There are two ways to sort HashMap by keys, first by using TreeMap and second by using LinkedHashMap . If you want to sort using TreeMap then it’s simple, just create a TreeMap by copying the content of HashMap.

Читайте также:  Тип переменной object java

On the other hand, if you want to create a LinkedHashMap then you first need to get a key set, convert that Set to a List, sort that List, and then add them into LHM in the same order. Remember HashMap can contain one null key but duplicate keys are not allowed.

HashMap Sorting by Keys and Values in Java Example

Here is our sample Java program to sort a HashMap first by keys and then by values. This program is divided into two part, the first part sorts HashMap by keys, and the second part sorts it by values. The second part is more tricky the first part as there is no native Map implementation that supports any order for values.

In order to sort a HashMap by values, we had to create our own Comparator implementation which compares each entry by values to arrange them in a particular order. You can see that our valueComparator overrides compare() method and accepts two entries. Later it retrieves values from those entries and compare them and return result.

Since there is no method in Java Collection API to sort Map, we need to use Collections.sort() method which accepts a List. This involves creating a temporary ArrayList with entries for sorting purpose and then again copying entries from sorted ArrayList to a new LinkedHashMap to keep them in sorted order.

Finally, we create a HashMap from that LinkedHashMap , which is what we needed.

import java.text.ParseException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; /** * How to sort HashMap in Java by keys and values. * HashMap doesn't guarantee any order, so you cannot rely on it, even if * it appear that it storing entries in a particular order, because * it may not be available in future version e.g. earlier HashMap stores * integer keys on the order they are inserted but from Java 8 it has changed. * * @author WINDOWS 8 */ public class HashMapSorting< public static void main(String args[]) throws ParseException < // let's create a map with Java releases and their code names HashMap codenames = new HashMap(); codenames.put("JDK 1.1.4", "Sparkler"); codenames.put("J2SE 1.2", "Playground"); codenames.put("J2SE 1.3", "Kestrel"); codenames.put("J2SE 1.4", "Merlin"); codenames.put("J2SE 5.0", "Tiger"); codenames.put("Java SE 6", "Mustang"); codenames.put("Java SE 7", "Dolphin"); System.out.println("HashMap before sorting, random order "); Set entries = codenames.entrySet(); for(Entry entry : entries)< System.out.println(entry.getKey() + " ==> " + entry.getValue()); > // Now let's sort HashMap by keys first // all you need to do is create a TreeMap with mappings of HashMap // TreeMap keeps all entries in sorted order TreeMap sorted = new TreeMap<>(codenames); Set mappings = sorted.entrySet(); System.out.println("HashMap after sorting by keys in ascending order "); for(Entry mapping : mappings)< System.out.println(mapping.getKey() + " ==> " + mapping.getValue()); > // Now let's sort the HashMap by values // there is no direct way to sort HashMap by values but you // can do this by writing your own comparator, which takes // Map.Entry object and arrange them in order increasing // or decreasing by values. Comparator valueComparator = new Comparator>() < @Override public int compare(Entry e1, Entry e2) < String v1 = e1.getValue(); String v2 = e2.getValue(); return v1.compareTo(v2); > >; // Sort method needs a List, so let's first convert Set to List in Java List listOfEntries = new ArrayList(entries); // sorting HashMap by values using comparator Collections.sort(listOfEntries, valueComparator); LinkedHashMap sortedByValue = new LinkedHashMap(listOfEntries.size()); // copying entries from List to Map for(Entry entry : listOfEntries)< sortedByValue.put(entry.getKey(), entry.getValue()); > System.out.println("HashMap after sorting entries by values "); Set entrySetSortedByValue = sortedByValue.entrySet(); for(Entry mapping : entrySetSortedByValue)< System.out.println(mapping.getKey() + " ==> " + mapping.getValue()); > > > Output: HashMap before sorting, random order Java SE 7 ==> Dolphin J2SE 1.2 ==> Playground Java SE 6 ==> Mustang J2SE 5.0 ==> Tiger J2SE 1.3 ==> Kestrel J2SE 1.4 ==> Merlin JDK 1.1.4 ==> Sparkler HashMap after sorting by keys in ascending order J2SE 1.2 ==> Playground J2SE 1.3 ==> Kestrel J2SE 1.4 ==> Merlin J2SE 5.0 ==> Tiger JDK 1.1.4 ==> Sparkler Java SE 6 ==> Mustang Java SE 7 ==> Dolphin HashMap after sorting entries by values Java SE 7 ==> Dolphin J2SE 1.3 ==> Kestrel J2SE 1.4 ==> Merlin Java SE 6 ==> Mustang J2SE 1.2 ==> Playground JDK 1.1.4 ==> Sparkler J2SE 5.0 ==> Tiger

That’s all about how to sort HashMap by keys and values in Java. Remember, HashMap is not intended to keep entries in sorted order, so if you have a requirement to always keep entries in a particular order, don’t use HashMap instead use TreeMap or LinkedHashMap .

This method should only be used to cater to Adhoc needs where you receive a HashMap from some part of legacy code and you have to sort it first to process entries. If you have control of creating the Map initially prefer the right implementation of Map than just HashMap.

Источник

How to maintain insertion order of the elements in Java HashMap?

Elements returned from the HashMap while iterating over it might not be in the same order they were inserted. That is because HashMap does not guarantee to maintain the insertion order of the elements. As per the HashMap Java Doc:

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.

How to maintain the insertion order of the elements in Java HashMap?

We cannot. The HashMap class does not maintain the order of the elements. This means that It might not return the elements in the same order they were inserted into it. If the application needs the elements to be returned in the same order they were inserted, LinkedHashMap should be used.

The LinkedHashMap class implements a doubly linked list so that it can traverse through all the elements. As per LinkedHashMap Java Doc:

This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

In the scenario wherein the HashMap is returned from the third-party library which cannot be changed and application needs ordering of the elements contained in it, a LinkedHashMap object can be created from the HashMap object. The LinkedHashMap class has a special constructor for that purpose which takes a Map as an argument.

The above given constructor creates an object of the LinkedHashMap class with the same mappings specified in the original Map object.

Important Note:

The LinkedHashMap object created in this way maintains the insertion order in which the elements were returned from the HashMap while the creation of it (and not the order in which elements were inserted into original HashMap). Let’s see a small example to demonstrate that.

Источник

Оцените статью