Access HashMap Elements

  1. entrySet() - returns a set of all the key/value mapping of the map
  2. keySet() - returns a set of all the keys of the map
  3. values() - returns a set of all the values of the map


\begin{lstlisting}
import java.util.HashMap;
class Main {
public static void ma...
... ()
System.out.println(''Values : '' + numbers.values());
}
}
\end{lstlisting}

Output


\begin{lstlisting}
HashMap : { One =1 , Two =2 , Three =3}
Key / Value mappings ...
...=2 , Three =3]
Keys : [ One , Two , Three ]
Values : [1 , 2, 3]
\end{lstlisting}

Using get() and getOrDefault().

  1. get() - Returns the value associated with the specified key. Returns null if the key is not found.
  2. getOrDefault() - Returns the value associated with the specified key. Returns the specified default value if the key is not found.


\begin{lstlisting}
import java.util.HashMap;
class Main {
public static void ma...
... 5);
System.out.println(''Returned Number : '' + value2);
}
}
\end{lstlisting}

Output


\begin{lstlisting}
HashMap : { One =1 , Two =2 , Three =3}
Returned Number : 3
Returned Number : 5
\end{lstlisting}