Remove Element From a Java Map

JavaJavaBeginner
Practice Now

Introduction

In Java, Map is an interface part of the Collection framework that is used to collect elements into key and value pairs. In this step-by-step lab, you will learn how to remove an element from a Java Map.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/SystemandDataProcessingGroup -.-> java/xml_dom4j("`XML/Dom4j`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/hashmap("`HashMap`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117446{{"`Remove Element From a Java Map`"}} java/xml_dom4j -.-> lab-117446{{"`Remove Element From a Java Map`"}} java/classes_objects -.-> lab-117446{{"`Remove Element From a Java Map`"}} java/class_methods -.-> lab-117446{{"`Remove Element From a Java Map`"}} java/hashmap -.-> lab-117446{{"`Remove Element From a Java Map`"}} java/modifiers -.-> lab-117446{{"`Remove Element From a Java Map`"}} java/oop -.-> lab-117446{{"`Remove Element From a Java Map`"}} java/wrapper_classes -.-> lab-117446{{"`Remove Element From a Java Map`"}} java/identifier -.-> lab-117446{{"`Remove Element From a Java Map`"}} java/arrays -.-> lab-117446{{"`Remove Element From a Java Map`"}} java/data_types -.-> lab-117446{{"`Remove Element From a Java Map`"}} java/operators -.-> lab-117446{{"`Remove Element From a Java Map`"}} java/output -.-> lab-117446{{"`Remove Element From a Java Map`"}} java/strings -.-> lab-117446{{"`Remove Element From a Java Map`"}} java/variables -.-> lab-117446{{"`Remove Element From a Java Map`"}} java/system_methods -.-> lab-117446{{"`Remove Element From a Java Map`"}} end

Create a Java Map

To remove an element from the map, we first need to create a Java Map. In this step, we use HashMap to create a map and add some elements to it:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();

        map.put("A", 65);
        map.put("B", 66);
        map.put("C", 67);
        map.put("D", 68);
        map.put("E", 69);

        System.out.println("Original Map: " + map);
    }
}

Removing an Element using remove()

We can use the remove() function to remove an element from the map by specifying the key of the element we want to remove. The remove() function will return the value of the removed element. Here is an example:

int removedValue = map.remove("D");
System.out.println("Removed Element Value: " + removedValue);
System.out.println("Map after removing element: " + map);

Removing an Element using remove(key, value)

The remove(key, value) function removes the element only if both the specified key and value are present in the map. The function will return true if both the key and value are present in the map, otherwise, it returns false. Here is an example:

boolean isElementRemoved = map.remove("D", 68);
System.out.println("Is Element Removed? " + isElementRemoved);
System.out.println("Map after removing element: " + map);

Replacing an Element using replace()

Instead of removing an element and then adding a new one, we can use the replace() function to replace an element in the map. The replace() function replaces the key-value pair of the specified key with the specified value. Here is an example:

map.replace("D", 90);
System.out.println("Map after replacing element: " + map);

Replacing an Element using replace(key, oldValue, newValue)

The replace(key, oldValue, newValue) function replaces the value of the specified key only if the current value is equal to the specified old value. Here is an example:

map.replace("D", 68, 5);
System.out.println("Map after replacing element: " + map);

Run the Code

To run the code, open the terminal, navigate to the directory where the code file is and run the following command to compile and run the code:

javac Main.java && java Main

You will see the output for each step in the terminal.

Summary

In this lab, you have learned how to remove an element from the Java Map using different techniques. You can now modify and remove elements from the Java Map using the remove() and replace() functions by specifying the key and value of the element.

Other Java Tutorials you may like