Site icon JavaGoal

HashMap method in java

In java, the HashMap class has various method that helps to perform different options. The HashMap class inherits all the methods of the Map interface. So the HashMap class contains all the java map methods or java map function.

How to use HashMap put() method?

HashMap provides some methods that are used to add to elements in HashMap. We have HashMap put() method, java map put all, and putIfAbsent() method.

1. put(K key, V value) method: The put(key, value) method is used to insert the value in HashMap based on key. It returns the old value if the key already exists. For more details, you can read from here.

2. putAll(Map map) method: The putAll(map) method is used to insert all elements of the specified map in the current HashMap. It copies all the mapping(Key and value pairs) of the specified Map to the current HashMap. For more details, you can read from here.

3. putIfAbsent(K key, V value) method: The putIfAbsent(key, value) method is used to insert the given value with the given key in HashMap, If the specified key is not already present in HashMap. For more details, you can read from here.

How to get values from hashmap in java example

4. get(Object key) method: The get(key) method is used to get the value from HashMap based on the key. It returns value or null, based on the given key. For more details, you can read from here.

5. getOrDefault(Object key, V defaultValue) method: The getOrDefault(key, defaultValue) method is used to get the value from HashMap based on key. But if the key doesn’t exist, it returns deafultValue. For more details, you can read from here.

6. values(): This method is used to get all values of HashMap. It returns type is Collection. It returns Collection that contains all the values of HashMap. For more details, you can read from here.

How to remove the element by Java HashMap remove() method

7. remove(Object key) method: The remove(key) method is used to remove an entry from the HashMap for the specified key. It returns the element after removing from HashMap. For more details, you can read from here.

8 clear() method: This method is used to remove all the elements from the HashMap. Its return type is void. It means this method doesn’t return anything. For more details, you can read from here.

How to replace the value by HashMap replace() method

9. replace(K key, V value) method: The replace(key, value) method is used to replace the entry for the specified key only if it is currently mapped to some value. For more details, you can read from here.

10. replace(K key, V oldValue, V newValue) method: The replace(key, oldValue, newValue) method is used to replace the entry for the specified key only if it is currently mapped to some value. If oldValue doesn’t match with the associated key, then it does not replace the value. Its return type is boolean. For more details, you can read from here.

How to check given key and given value in HashMap

11. containsKey(Object key) method: The containsKey(Object key) method is used to check whether the specified key exists in HashMap or not. It returns type is boolean. If the given key exists in HashMap, then it returns true otherwise false. For more details, you can read from here.

How to use the Java map entryset() method

12. entrySet() method This method is used to fetch the Set view of all keys and Values. It returns all the key and values in the form of Set. This Set worked on backed of HashMap, if any change makes in HashMap also reflects in this Set and vice versa. It returns type is Set. For more details, you can read from here.

How to use the hashmap keyset() method?

12. keySet() method: The keySet() method is used to fetch the Set view of all keys. It returns all the key set. This Set worked on backed of HashMap, if any change makes in HashMap also reflects in this Set and vice versa. It returns type is Set. For more details, you can read from here.

Some other methods

13. isEmpty() method: This method is used to check whether the HashMap contains an element or not. Basically, it checks the size of the elements. 
return true, if HashMap doesn’t contain any element return false if HashMap contains any element.

import java.util.HashMap;

public class HashMapExample
{
   public static void main(String args[])
   {
	   HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
	   hashMap.put(1, "JavaGoal.com");
	   hashMap.put(2, "Learning");
	   hashMap.put(3, "Website");
	   
	   System.out.println("Is HashMap Empty: "+ hashMap.isEmpty());
	   // Removing all objects from HashMap
	   hashMap.clear();
	   System.out.println("Is HashMap Empty: "+ hashMap.isEmpty());
	   
   }
}

Output: Is HashMap Empty: false
Is HashMap Empty: true

15. size() method: It is used to check the number of key-value pair present in HashMap. Its return type is int.

import java.util.HashMap;

public class HashMapExample
{
   public static void main(String args[])
   {
	   HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
	   hashMap.put(1, "JavaGoal.com");
	   hashMap.put(2, "Learning");
	   hashMap.put(3, "Website");
	   
	   System.out.println("Size of HashMap: "+ hashMap.size());
	   // Removing all objects from HashMap
	   hashMap.clear();
	   System.out.println("Size of HashMap: "+ hashMap.size());
   }
}

Output: Size of HashMap: 3
Size of HashMap: 0

16. containsValue(Object value) method This method is used to check whether the specified Value exists in HashMap or not. It returns type is boolean. If the given value exists in HashMap, then it returns true otherwise false.

containsValue(Object value);

Where, Value is the  value which you want to check in HashMap.
return type:  Its return type is boolean. It can return either true or false.

import java.util.HashMap;

public class HashMapExample
{
   public static void main(String args[])
   {
	   HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
	   hashMap.put(1, "JavaGoal.com");
	   hashMap.put(2, "Learning");
	   hashMap.put(3, "Website");
	   
	   System.out.println("Is Value contained: "+ hashMap.containsValue("Website"));
	   System.out.println("Is Value contained: "+ hashMap.containsValue("Hello"));

   }
}

Output: Is Value contained: true
Is Value contained: false

Exit mobile version