HashMap is a class which implements Map interface.
Map collection used to store data in key value pair.
It doesn’t maintain insertion order which means there is no guarantee for the order of key value pair while accessing the HashMap.
HashMap is similar to the Hashtable class except HashMap allows multiple null values and the null key.
HashMap contains only unique keys. The addition of a duplicate key will not throw an exception and will replace it with the last added item.
Also, the HashMap is unsynchronized which means multiple threads can access the HashMap at the same time and change its value in comparison of Hashtable which is synchronized and make it as a low performer as single thread can access Hashtable at the same time.
HashMap belongs to java.util.HashMap package.
HashMap<K, V>()
HashMap<K, V>(int initialCapacity, loadFactor)
HashMap provides below four contructors to create its insatance:
Example:
HashMap Methods:
Below are the methods define in HashMap which will help us to add, delete, get, contains, clear etc. values in HashMap.
For sorting a Map we will be using List which provides us Collections.sort() method for sorting elements within the list. Comparator interface from java.util.* package will help us to compare or sort values using compare method.
Below code is performing sorting on map values.
Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.
📝 Blogs
javadoubts.com © All rights reserved