![]() |
VOOZH | about |
HashMap<Key, Value> provides the basic implementation of the Map interface of Java and import java.util.HashMap package or its superclass. HashMap stores the data in (Key, Value) pairs, and accessed by an index of another type (e.g. an Integer). One object is used as a key to another object. If the duplicate key is inserted, it will replace the element of the corresponding key.
Approach :
Example:
{Bob=1, Alice=1, and=1, is=2, girl=1, boy=1}Note that in the above code, we have used Wrapper class i.e. Integer, since the get(key) method returns null.
You can also eliminate the use of integer variable from the above program.
Let us see the code below :
{Bob=1, Alice=1, and=1, is=2, girl=1, boy=1}