HashMap remove() Method in Java

Published by user on

The remove(K key) method of HashMap removes the entry for the specified key from the map. It returns the value associated with the key.

To remove a specific key-value combination from the HashMap use the remove(K key, V value) method. The remove(key, value) method removes the entry only when both key and value are present in the HashMap. If the key is associated with a different value then the HashMap doesn’t remove the entry.

remove(key) Method Signature

public V remove(K key)

Parameters

The key to be removed from the HashMap

Return Value

It returns the value for the specified key

remove(key, value) Method Signature

public boolean remove(K key, V value)

Parameters

The key and value i.e entry that need to be removed from the map.

Return Value

Returns true if the entry gets removed or false

Program 1

import java.util.HashMap;
import java.util.Map;

// This program shows
// how to use remove(key) method
// to remove an entry based on the specified key
public class HashMapRemove {
	public static void main(String[] args) {
		// Create an instance of HashMap
		Map<Integer, String> map = new HashMap<>();
		// put entries in the it
		map.put(101, "John");
		map.put(102, "Adam");
		map.put(103, "Ricky");
		map.put(104, "Chris");

		System.out.println("Map elements : " + map);
		// Remove entry with key 104
		map.remove(104);
		System.out.println("Map elements aftre removing 104 : " + map);
		// Remove method returns null if the entry
		// with the specified key is not present
		System.out.println("Entry not presen with key 105 : " + map.remove(105));
	}
}

Output

Map elements : {101=John, 102=Adam, 103=Ricky, 104=Chris}
Map elements aftre removing 104 : {101=John, 102=Adam, 103=Ricky}
Entry not presen with key 105 : null

Explanation

  1. Create an instance of HashMap
  2. Put entries with the keys 101, 102, 103, and 104
  3. At this point, we have four entries in the HashMap
  4. Remove the entry with the key 104, as 104 is present on the map the entry will be successfully removed
  5. Try to remove the entry with 105, as this entry is not present in the HashMap. So the map remains unchanged.

Program 2

import java.util.HashMap;
import java.util.Map;

// This program shows
// how to use remove(key, value) method
// to remove an entry only when
// the key is associated with the specified value
public class HashMapRemoveKeyValuePair {
	public static void main(String[] args) {
		// Create an instance of HashMap
		Map<Integer, String> map = new HashMap<>();
		// put entries in the map
		map.put(101, "John");
		map.put(102, "Adam");
		map.put(103, "Ricky");
		map.put(104, "Chris");

		System.out.println("Map elements : " + map);
		// (102, Adam) combination is present in HashMap.
		// So this call returns true
		boolean is102AdamRemoved = map.remove(102, "Adam");
		System.out.println("Is (102, Adam) removed? " + is102AdamRemoved);

		System.out.println("After removing entry with key 102 : " + map);
		// (106, Ricky) combination is not present in HashMap.
		// So this call returns false
		System.out.println(map.remove(106, "Ricky"));
		// Also 103 is present in HashMap
		// but is associated with Ricky
		// So the below call returns false
		System.out.println(map.remove(103, "John"));
	}
}

Output

Map elements : {101=John, 102=Adam, 103=Ricky, 104=Chris}
Is (102, Adam) removed? true
After removing entry with key 102 : {101=John, 103=Ricky, 104=Chris}
false
false

Explanation

  1. Create an instance of HashMap
  2. Put entries with the keys 101, 102, 103, and 104
  3. At this point, we have four entries in the HashMap
  4. Remove the entry with the (102, Adam) combination. As this entry is present, it will be removed.
  5. Try to remove the entry with (106, Ricky) combination. As this entry is not present in the HashMap. Therefore the map remains unchanged.
  6. Try to remove the entry with (103, John) combination. As the key 103 is present but it is associated with Ricky and not John. Therefore the map will remain unchanged.
Categories: Java

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *