HashMap Operations
Initializing the HashMap.
Below are some ways to initialize HashMap in java.
HashMap<String><Integer> map= new HashMap<String><Integer>(); map.put(“Invoice”, 13200); map.put(“Payment1”, 2700); map.put(null, 0); map.putIfAbsent(“priority”, 1);
HashMap<Integer><Integer> ints= new HashMap<Integer><Integer>(); ints.put(3, 21000); ints.put(7,49000);
The HashMap put() method returns null after adding the key-value if the input key did not already exist in the HashMap and returns the old value if the key already existed in the HashMap.
HashMap class has around 4 constructors that can be used to create and initialize HashMaps. These constructors take HashMaps capacity and load factor as inputs. We can construct the HashMap using one of these constructors as needed.
The HashMap putAll() method takes a HashMap as an input and adds all elements of the input HashMap to the current HashMap. Duplicate keys from current HashMap, if any, will be overwritten.
Size of the HashMap.
The size of the HashMap (that is the number of key-value in the HashMap) can be accessed using the size() function as shown below:
int mapSize= map.size();
Searching within a HashMap.
The HashMap containsKey() method can be used to search for required key in the HashMap. The HashMap returns true if the element is found in the HashMap. Similarly, HashMap containsValue() method can be used to search for required value in the HashMap.
boolean checkK = map.containsKey(“currency”); boolean checkM = map.containsValue(“$”);
Accessing the HashMaps key-value pairs.
To access an individual value mapped to a key, we can use the get() method. The get() method return null if the value is not found in the HashMap:
map.get(“Invoice”);
The getorDefault(key, default) method returns the default value if the key is not found in the HashMap:
map.get(“Invoice”, 0);
To access the entire HashMaps keys and corresponding value mappings, we need to use an iterator as shown below:
Iterator itr=map.keySet().iterator();
while (itr.hasNext()) {
String key= itr.next();
System.out.println(key+ “ “ + map.get(key));
}
We can also use the HashMap values() method to access the set of all values in the HashMap. Another option is to use the HashMap entrySet() method to access all the key-value mappings present in the HashMap.
Removing elements from the HashMap.
We can remove elements from the HashMap using the HashSet remove() method. The remove() methods returns mapped value if the key was found within the HashMap, else it returns null.
map.remove(“Payment”);
The HashMap remove(key, value) method removed mapping from the HashMap only if the input key value is mapped to input value. The method returns true if the mapping was found and removed, else it returns false.
Other HashMap implementations.
In addition to HashMap class, java also provides the following map implementations:
ConcurrentHashMap : it is a thread safe implementation of HashMap.
LinkedHashMap: preserves the insertion order of the keys of the HashMap.
TreeMap: allows keys of the HashMap to be sorted.
IdentityHashMap: uses reference equality of keys and values to check equality, rather than object equality.
| About Us | Privacy Policy | Contact us |