Java HashMap Hierarchy
public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {
//More Code
}
Java HashMap Features
Hashmap java have some specific characteristics as below .
- Java HashMap contains only unique keys.
- HashMap in Java may have multiple null values but only one null key.
- HashMap Java is an unordered collection.It does not give any guarantee of specific order of the elements.
- Java HashMap is non-synchronize so it is not thread-safe. You must explicitly synchronize concurrent modifications to the HashMap as
Map m = Collections.synchronizedMap(new HashMap(…)); - The initial default capacity of Java HashMap is 16 and load factor is 0.75.
- HashMap In java implements Cloneable and Serializable interfaces.
- If try to add duplicate key , it will update value of key in hashmap.
Create hashmap java
HashMap(int initialCapacity) – Constructs an empty HashMap in java with the specified initial capacity and the default load factor (0.75).
HashMap(int initialCapacity, float loadFactor) – Constructs an empty Java HashMap with the specified initial capacity and load factor.
HashMap(Map<? extends K,? extends V> m) – Constructs a new HashMap with the same mappings as the specified Map. This is a methods to create map with values in java .
package com.demo;
import java.util.HashMap;
import java.util.Map;
public class HashMapExample {
public static void main(String[] args) {
/*
*Constructs an empty HashMap with the default initial capacity (16)
*and the default load factor (0.75).
*/
Map<String, String> hashMap = new HashMap<String, String>();
/*
*Associates the specified value with the specified key in
this map (optional operation). If the map previously
contained a mapping for the key, the old value is replaced
by thespecified value.(A map m is said to contain a
mapping for a key k if and only if m.containsKey(k)
would return true.)
*/
hashMap.put("hcl", "amit");
hashMap.put("tcs","ravi");
hashMap.put("wipro","anmol");
//display the element of hashmap
System.out.println("element in hashmap ="+ hashMap);
}
}
Output :
element in hashmap ={hcl=amit, tcs=ravi, wipro=anmol}
In this java hashmap example we have initialized hashmap in java using constructor and then put element in map . for more details see create hashmap java with examples .
How to iterate hashmap in java
We can iterate hashmap in java using entrySet , keyset with the help of for each loop or java Iterator . Let’s see how java iterating hashmap and get keys-values pairs in following java hashmap example .
package com.demo;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class HashMapExample {
public static void main(String[] args) {
/*
*Constructs an empty HashMap with the default initial capacity (16)
*and the default load factor (0.75).
*/
Map<String, String> hashMap = new HashMap<String, String>();
/*
*Associates the specified value with the specified
*key in this map (optional operation).
*If the map previously contained a mapping for the key,
* the old value is replaced by the specified value.
*(A map m is said to contain a mapping for a key k
* if and only if m.containsKey(k) would return true.)
*/
hashMap.put("hcl", "amit");
hashMap.put("tcs","ravi");
hashMap.put("wipro","anmol");
System.out.println("====>> Iterating HashMap using entrySet ===>>");
Set<Map.Entry<String, String>> employeeEntries = hashMap.entrySet();
Iterator<Map.Entry<String, String>> employeeIterator = employeeEntries.iterator();
while (employeeIterator.hasNext()) {
Map.Entry<String, String> entry = employeeIterator.next();
System.out.println("Key Of map = "+ entry.getKey() + " ,
value of map = " + entry.getValue() );
}
System.out.println("===>> Iterating HashMap using for-each loop ===>>");
for(Map.Entry<String, String> entry: hashMap.entrySet()) {
System.out.println("Key Of map = "+ entry.getKey()
+ " , value of map = " + entry.getValue() );
}
}
}
====>> Iterating HashMap using entrySet ===>>
Key Of map = hcl , value of map = amit
Key Of map = tcs , value of map = ravi
Key Of map = wipro , value of map = anmol
===>> Iterating HashMap using entrySet with simple for-each loop ===>>
Key Of map = hcl , value of map = amit
Key Of map = tcs , value of map = ravi
Key Of map = wipro , value of map = anmol
Hashmap methods in java
Now we will discuss various hashmap methods in java . Hashmap in java have inbuild methods, these hashmap java methods are easy to use and make developer work easy . You should once reads about java map methods as following .
- void clear() – It removes all of the mappings from this map.
- Object clone() – it rreturns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
- boolean containsKey(Object key) – It returns true if this map contains a mapping for the given key.
- boolean containsValue(Object value) – it returns true if this map maps one or more keys to the given value.
- Set<Map.Entry<K,V>> entrySet() – It returns a Set view of the mappings contained in this map.
- V get(Object key) – It returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
- V getOrDefault(Object key, V defaultValue) – It returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
- boolean isEmpty() – It returns true if this map contains no key-value mappings.
- Set<K> keySet() – It returns a Set view of the keys contained in this map
Java hashmap example
Now we will see nice java hashmap example collection . These java map example are well explained with simple java programs .
- How to create HashMap In Java
- How to iterate through HashMap
- How to storing Java Object In HashMap
- How to Search key in HashMap Java
- How to Search Value in HashMap Java
- How to get value of key in HashMap
- How to get all keys of HashMap
- How to get size of HashMap
- How to get entry set from HashMap
- How to delete all elements from HashMap
- How to Print Hashmap In Java