- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort LinkedHashMap by Keys in Java
LinkedHashMap is a generic class that is used to implement Map Interface. Also, it is a sub class of the HashMap class therefore, it can use all the methods and also perform similar operations that a HashMap class is capable of.
Java provides various ways to sort LinkedHashMap, we will learn how to create and sort it by using its keys through this article.
Program to sort LinkedHashMap by Keys
Before jumping to the sorting program directly, let’s take a look at few concepts first −
LinkedHashMap
As we have discussed earlier the LinkedHashMap class extends HashMap class to implement Map Interface. It maintains Key-Value pair. The Key is an object that is used to fetch and receive value associated with it. It stores the elements of the map in LinkedList in the order in which they are inserted i.e. it maintains the insertion order of elements. Also, whenever we return its element it will be printed in the insertion order.
The general syntax for LinkedHashMap is as follows −
Syntax
LinkedHashMap< TypeOfKey, TypeOfValue > nameOfMap = new LinkedHashMap<>();
In the above syntax,
TypeOfKey − Specify the datatype of Keys.
TypeOfValue − Specify the datatype of values that are going to be stored in map.
nameOfMap − Give a suitable name to your map.
Tree Map
It is a class that is used to implement NavigableMap Interface. It stores the elements of the map in a tree structure. To sort the LinkedHashMap elements we need to use this class. The most obvious reason for this is that it provides an efficient alternative to store the key-value pairs in sorted order.
The general syntax for TreeMap is as follows −
Syntax
TreeMap< TypeOfKey, TypeOfValue > nameOfMap = new TreeMap<>();
To sort the elements of LinkedHashMap, we require these methods also −
put() − It takes two arguments one for key and the other for its value, then insert them into the specified map.
get() − It returns the value associated with the specified key.
keyset() − It returns the set of keys of specified map.
putAll() − It takes an argument and copies its element to a new specified map.
Algorithm
Step 1 − Create an object of LinkedHashMap named ‘workers’ and use the ‘put()’ method that will insert elements to it.
Step 2 − Now, define a TreeMap named ‘SrtMap’ to store the sorted elements of map ‘workers’.
Step 3 − At the end, take a for each loop that will iterate through the sorted TreeMap ‘SrtMap’. Inside this loop, use ‘keySet()’ method to retrieve the values.
Example
import java.util.*; public class Srt { public static void main(String[] args) { LinkedHashMap<String, Integer> workers = new LinkedHashMap<>(); // Adding elements in the workers map workers.put("Vaibhav", 4000); workers.put("Ansh", 3000); workers.put("Vivek", 1500); workers.put("Aman", 2000); workers.put("Tapas", 2500); // printing details workers map in unsorted order System.out.println("Elements of the map: "); for (String unKey : workers.keySet()) { System.out.println("Name: " + unKey + ", Salary: " + workers.get(unKey)); } // create new tree map TreeMap<String, Integer> SrtMap = new TreeMap<>(); // adding workers map details to new tree map SrtMap.putAll(workers); // printing details workers map in sorted order System.out.println("Elements of the newly sorted map: "); for (String srtKey : SrtMap.keySet()) { System.out.println("Name: " + srtKey + ", Salary: " + workers.get(srtKey)); } } }
Output
Elements of the map: Name: Vaibhav, Salary: 4000 Name: Ansh, Salary: 3000 Name: Vivek, Salary: 1500 Name: Aman, Salary: 2000 Name: Tapas, Salary: 2500 Elements of the newly sorted map: Name: Aman, Salary: 2000 Name: Ansh, Salary: 3000 Name: Tapas, Salary: 2500 Name: Vaibhav, Salary: 4000 Name: Vivek, Salary: 1500
Conclusion
In this article, we created a LinkedHashMap and through TreeMap we sorted it by keys. We also discovered the use of a few inbuilt methods of Map Interface such as put, putAll, get and so forth.