0% found this document useful (0 votes)
23 views6 pages

Hash Map

The Java HashMap class implements the Map interface, allowing storage of unique key-value pairs, where duplicate keys replace existing values. It is non-synchronized, can contain one null key, and has an initial capacity of 16 with a load factor of 0.75. Key methods include put, get, remove, and various computation functions for managing entries.

Uploaded by

sharmila.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

Hash Map

The Java HashMap class implements the Map interface, allowing storage of unique key-value pairs, where duplicate keys replace existing values. It is non-synchronized, can contain one null key, and has an initial capacity of 16 with a load factor of 0.75. Key methods include put, get, remove, and various computation functions for managing entries.

Uploaded by

sharmila.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Java HashMap

Java HashMap class implements the Map interface which allows us to store key and value
pair, where keys should be unique. If you try to insert the duplicate key, it will replace the
element of the corresponding key. It is easy to perform operations using the key index like
updation, deletion, etc. HashMap class is found in the java.util package.

HashMap in Java is like the legacy Hashtable class, but it is not synchronized. It allows us to
store the null elements as well, but there should be only one null key. Since Java 5, it is
denoted as HashMap<K,V>, where K stands for key and V for value. It inherits the
AbstractMap class and implements the Map interface.

Points to remember

o Java HashMap contains values based on the key.


o Java HashMap contains only unique keys.
o Java HashMap may have one null key and multiple null values.
o Java HashMap is non synchronized.
o Java HashMap maintains no order.
o The initial default capacity of Java HashMap class is 16 with a load
factor of 0.75.

Hierarchy of HashMap class


As shown in the above figure, HashMap class extends AbstractMap class and implements
Map interface.

HashMap class declaration


Let's see the declaration for java.util.HashMap class.

Backward Skip 10sPlay VideoForward Skip 10s


1. public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>,
Cloneable, Serializable

HashMap class Parameters


Let's see the Parameters for java.util.HashMap class.

o K: It is the type of keys maintained by this map.


o V: It is the type of mapped values.

Constructors of Java HashMap class

Constructor Description

HashMap() It is used to construct a default HashMap.

HashMap(Map<? extends It is used to initialize the hash map by


K,? extends V> m) using the elements of the given Map
object m.

HashMap(int capacity) It is used to initializes the capacity of the


hash map to the given integer value,
capacity.

HashMap(int capacity, It is used to initialize both the capacity and


float loadFactor) load factor of the hash map by using its
arguments.

Methods of Java HashMap class

Method Description

void clear() It is used to remove all of the


mappings from this map.

boolean isEmpty() It is used to return true if this map


contains no key-value mappings.

Object clone() It is used to return a shallow copy of


this HashMap instance: the keys and
values themselves are not cloned.

Set entrySet() It is used to return a collection view


of the mappings contained in this
map.
Set keySet() It is used to return a set view of the
keys contained in this map.

V put(Object key, Object It is used to insert an entry in the


value) map.

void putAll(Map map) It is used to insert the specified map


in the map.

V putIfAbsent(K key, V value) It inserts the specified value with the


specified key in the map only if it is
not already specified.

V remove(Object key) It is used to delete an entry for the


specified key.

boolean remove(Object key, It removes the specified values with


Object value) the associated specified keys from
the map.

V compute(K key, It is used to compute a mapping for


BiFunction<? super K,? super the specified key and its current
V,? extends V> mapped value (or null if there is no
remappingFunction) current mapping).

V computeIfAbsent(K key, It is used to compute its value using


Function<? super K,? extends the given mapping function, if the
V> mappingFunction) specified key is not already
associated with a value (or is
mapped to null), and enters it into
this map unless null.

V computeIfPresent(K key, It is used to compute a new mapping


BiFunction<? super K,? super given the key and its current mapped
V,? extends V> value if the value for the specified
remappingFunction) key is present and non-null.

boolean containsValue(Object This method returns true if some


value) value equal to the value exists within
the map, else return false.

boolean containsKey(Object This method returns true if some key


key) equal to the key exists within the
map, else return false.

boolean equals(Object o) It is used to compare the specified


Object with the Map.

void forEach(BiConsumer<? It performs the given action for each


super K,? super V> action) entry in the map until all entries
have been processed or the action
throws an exception.

V get(Object key) This method returns the object that


contains the value associated with
the key.

V getOrDefault(Object key, V It returns the value to which the


defaultValue) specified key is mapped, or
defaultValue if the map contains no
mapping for the key.

boolean isEmpty() This method returns true if the map


is empty; returns false if it contains
at least one key.

V merge(K key, V value, If the specified key is not already


BiFunction<? super V,? super associated with a value or is
V,? extends V> associated with null, associates it
remappingFunction) with the given non-null value.

V replace(K key, V value) It replaces the specified value for a


specified key.

boolean replace(K key, V It replaces the old value with the new
oldValue, V newValue) value for a specified key.

void replaceAll(BiFunction<? It replaces each entry's value with


super K,? super V,? extends the result of invoking the given
V> function) function on that entry until all entries
have been processed or the function
throws an exception.

Collection<V> values() It returns a collection view of the


values contained in the map.

int size() This method returns the number of


entries in the map.

Java HashMap Example


Let's see a simple example of HashMap to store key and value pair.

1. import java.util.*;
2. public class HashMapExample1{
3. public static void main(String args[]){
4. HashMap<Integer,String> map=new HashMap<Integer,String>();//Creating HashM
ap
5. map.put(1,"Mango"); //Put elements in Map
6. map.put(2,"Apple");
7. map.put(3,"Banana");
8. map.put(4,"Grapes");
9.
10. System.out.println("Iterating Hashmap...");
11. for(Map.Entry m : map.entrySet()){
12. System.out.println(m.getKey()+" "+m.getValue());
13. }
14. }
15. }
Test it Now
Iterating Hashmap...
1 Mango
2 Apple
3 Banana
4 Grapes

In this example, we are storing Integer as the key and String as the value, so we are
using HashMap<Integer,String> as the type. The put() method inserts the elements in the
map.

To get the key and value elements, we should call the getKey() and getValue() methods.
The Map.Entry interface contains the getKey() and getValue() methods. But, we should call
the entrySet() method of Map interface to get the instance of Map.Entry.

No Duplicate Key on HashMap


You cannot store duplicate keys in HashMap. However, if you try to store duplicate key with
another value, it will replace the value.

1. import java.util.*;
2. public class HashMapExample2{
3. public static void main(String args[]){
4. HashMap<Integer,String> map=new HashMap<Integer,String>();//Creating HashM
ap
5. map.put(1,"Mango"); //Put elements in Map
6. map.put(2,"Apple");
7. map.put(3,"Banana");
8. map.put(1,"Grapes"); //trying duplicate key
9.
10. System.out.println("Iterating Hashmap...");
11. for(Map.Entry m : map.entrySet()){
12. System.out.println(m.getKey()+" "+m.getValue());
13. }
14. }
15. }
Test it Now
Iterating Hashmap...
1 Grapes
2 Apple
3 Banana

You might also like