Collections

HashMapSorting.java

package HashMap;

import java.util.ArrayList;
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;

public class HashMapSorting {
                public static void main(String[] args) {
                                // Let's create a map with java releases and their code names
                                HashMap<String, String> codenames = new HashMap<String, String>();
                                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");
                                Set<Entry<String, String>> entries = codenames.entrySet();
                                System.out.println("HashMap before sorting random order:-");
                                for (Entry<String, String> entry : entries) {
                                                System.out.println(entry.getKey() + entry.getValue());
                                }

                                System.out.println();
                                // Now let's sort HashMap by keys first
                                // Create TreeMap with mappings of HashMap
                                // TreeMap keeps all entries in sorted order
                                TreeMap<String, String> sorted = new TreeMap<>(codenames);
                                Set<Entry<String, String>> mappings = sorted.entrySet();
                                System.out
                                                                .println("HashMap after sorting by keys in ascending order:-");

                                for (Entry<String, String> mapping : mappings) {
                                                System.out.println(mapping.getKey() + mapping.getValue());
                                }

                                System.out.println();
                                // Now lets sort HashMap by values
                                // There is no direct way to sort HashMap by values
                                // we can do using comparator, which takes Map.Entry object and arrange
                                // them in increasing (or) decreasing by values.
                                Comparator<Entry<String, String>> valueComparator = new Comparator<Entry<String, String>>() {

                                                @Override
                                                public int compare(Entry<String, String> e1,
                                                                                Entry<String, String> e2) {
                                                                String v1 = e1.getValue();
                                                                String v2 = e2.getValue();
                                                                return v1.compareTo(v2);
                                                }// compare()
                                };// Comparator interface

                                // Sort method needs a List, so first convert Set to List in Java
                                List<Entry<String, String>> listOfEntries = new ArrayList<Entry<String, String>>(
                                                                entries);
                                // sorting HashMap by values using comparator
                                LinkedHashMap<String, String> sortedByValue = new LinkedHashMap<String, String>(
                                                                listOfEntries.size());
                                // copying entries from List to Map
                                for (Entry<String, String> entry : listOfEntries) {
                                                sortedByValue.put(entry.getKey(), entry.getValue());
                                }

                                System.out.println("HashMap after sorting entriess by values ");
                                Set<Entry<String, String>> entrySetSortedByValue = sortedByValue
                                                                .entrySet();
                                for (Entry<String, String> mapping : entrySetSortedByValue) {
                                                System.out.println(mapping.getKey() + mapping.getValue());
                                }
                }// main()
}// class


/*
 * 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
 * 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 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 then just
 * HashMap.
 */

O/p:-
HashMap before sorting random order:-
Java SE 7Dolphin
J2SE 1.2Playground
Java SE 6Mustang
J2SE 5.0Tiger
J2SE 1.3Kestrel
J2SE 1.4Merlin
JDK 1.1.4Sparkler

HashMap after sorting by keys in ascending order:-
J2SE 1.2Playground
J2SE 1.3Kestrel
J2SE 1.4Merlin
J2SE 5.0Tiger
JDK 1.1.4Sparkler
Java SE 6Mustang
Java SE 7Dolphin

HashMap after sorting entriess by values
Java SE 7Dolphin
J2SE 1.2Playground
Java SE 6Mustang
J2SE 5.0Tiger
J2SE 1.3Kestrel
J2SE 1.4Merlin
JDK 1.1.4Sparkler

No comments:

Post a Comment