java hashmap
java hashmap
Create a HashMap
• In order to create a hash map, we must import the java.util.HashMap package
first.
• Once we import the package, here is how we can create hashmaps in Java.
Here, the type of keys is String and the type of values is Integer.
Example 1: Create HashMap in Java
import java.util.HashMap;
class Main {
public static void main(String[] args) {
// create a hashmap
HashMap<String, Integer> languages = new HashMap<>();
• Access elements
• Change elements
• Remove elements
class Main {
public static void main(String[] args) {
// create a hashmap
HashMap<String, Integer> numbers = new HashMap<>();
Initial HashMap: {}
HashMap after put(): {One=1, Two=2, Three=3}
We can use the get() method to access the value from the hashmap. For
example,
import java.util.HashMap;
class Main {
public static void main(String[] args) {
HashMap<Integer, String> languages = new HashMap<>();
languages.put(1, "Java");
languages.put(2, "Python");
languages.put(3, "JavaScript");
System.out.println("HashMap: " + languages);
languages.get(1);
Here, the get() method takes the key as its argument and returns the
corresponding value associated with the key.
We can also access the keys, values, and key/value pairs of the
hashmap as set views using keySet(), values(), and entrySet() methods
respectively. For example,
import java.util.HashMap;
class Main {
public static void main(String[] args) {
HashMap<Integer, String> languages = new HashMap<>();
languages.put(1, "Java");
languages.put(2, "Python");
languages.put(3, "JavaScript");
System.out.println("HashMap: " + languages);
We can use the replace() method to change the value associated with a
key in a hashmap. For example,
import java.util.HashMap;
class Main {
public static void main(String[] args) {
HashMap<Integer, String> languages = new HashMap<>();
languages.put(1, "Java");
languages.put(2, "Python");
languages.put(3, "JavaScript");
System.out.println("Original HashMap: " + languages);
languages.replace(2, "C++");
Here, we are changing the value referred to by key 2 with the new
value C++.
4. Remove HashMap Elements
class Main {
public static void main(String[] args) {
Here, the remove() method takes the key as its parameter. It then
returns the value associated with the key and removes the entry.
We can also remove the entry only under certain conditions. For
example,
remove(2, "C++");
Here, the remove() method only removes the entry if the key 2 is
associated with the value C++. Since 2 is not associated with C++, it
doesn't remove the entry.
class Main {
public static void main(String[] args) {
// create a HashMap
HashMap<Integer, String> languages = new HashMap<>();
languages.put(1, "Java");
languages.put(2, "Python");
languages.put(3, "JavaScript");
System.out.println("HashMap: " + languages);
Note that we have used the Map.Entry in the above example. It is the
nested class of the Map interface that returns a view (elements) of the
map.
We first need to import the java.util.Map.Entry package in order to use
this class.
This nested class returns a view (elements) of the map.
Creating HashMap from Other Maps
In Java, we can also create a hashmap from other maps. For example,
import java.util.HashMap;
import java.util.TreeMap;
class Main {
public static void main(String[] args) {
// create a treemap
TreeMap<String, Integer> evenNumbers = new TreeMap<>();
evenNumbers.put("Two", 2);
evenNumbers.put("Four", 4);
System.out.println("TreeMap: " + evenNumbers);
Here,