Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Map clear() method in Java with Example
Next article icon

Map remove() Method in Java with Examples

Last Updated : 11 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The map.remove() method is used to remove the mapping for a key from this map if it is present in the map. 

Syntax:  

V remove(Object key)
  • Parameters: This method has the only argument key, whose mapping is to be removed from the map.
  • Returns: This method returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.
  • Time Complexity: Average time complexity of O(1) and a worst-case time complexity of O(n).

Map remove() Method in Java

Example 1: The below programs show the implementation of the map.remove() method.

Java
// Java code to show the implementation of // remove method in Map interface import java.util.*; public class GfG {      // Driver code     public static void main(String[] args)     {          // Initializing a Map of type HashMap         Map<Integer, String> map = new HashMap<>();         map.put(1, "One");         map.put(3, "Three");         map.put(5, "Five");         map.put(7, "Seven");         map.put(9, "Nine");         System.out.println(map);          map.remove(3);          System.out.println(map);          // If it doesn't exists, returns         // null and does not affects the map         map.remove(2);          System.out.println(map);     } } 

Output
{1=One, 3=Three, 5=Five, 7=Seven, 9=Nine} {1=One, 5=Five, 7=Seven, 9=Nine} {1=One, 5=Five, 7=Seven, 9=Nine} 

Explanation of the Program:

  • This example demonstrates the use of the remove method in the Map interface using a HashMap.
  • It initializes a HashMap with integer keys and string values, then removes the entry with key 3, and attempts to remove a non-existent key 2.
  • The output shows the map before and after the removals, illustrating how the remove method works and that attempting to remove a non-existent key has no effect on the map.

Example 2: Below is the code to show implementation of put(). 

Java
// Java code to show the implementation of // remove method in Map interface import java.util.*; public class GfG {      // Driver code     public static void main(String[] args)     {          // Initializing a Map of type HashMap         Map<String, String> map = new HashMap<>();         map.put("1", "One");         map.put("3", "Three");         map.put("5", "Five");         map.put("7", "Seven");         map.put("9", "Nine");         System.out.println(map);          map.remove("3");          System.out.println(map);     } } 

Output
{1=One, 3=Three, 5=Five, 7=Seven, 9=Nine} {1=One, 5=Five, 7=Seven, 9=Nine}

Explanation of the Program:

  • This program demonstrates the use of the remove method in the Map interface using a HashMap.
  • It also initializes a HashMap with string keys and values, prints the map, removes the entry with key "3", and then prints the map again to show the result of the removal.
  • The output illustrates how the remove method affects the map by removing the specified key-value pair.



Next Article
Map clear() method in Java with Example

B

barykrg
Improve
Article Tags :
  • Java
  • Java-Collections
  • Java - util package
  • Java-Functions
  • java-map
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

    Map Interface in Java
    In Java, the Map Interface is part of the java.util package and represents a mapping between a key and a value. The Java Map interface is not a subtype of the Collections interface. So, it behaves differently from the rest of the collection types.Key Features:No Duplicates in Keys: Keys should be un
    11 min read
    Map put() Method in Java with Examples
    The Map put() method associates the specified value with the specified key in the map. The map put() method is used to insert new key-value pairs inside a map in Java. If the map already contains the mapping for the specified key, the old value is replaced by the new specified value. Example: Java /
    3 min read
    Map remove() Method in Java with Examples
    The map.remove() method is used to remove the mapping for a key from this map if it is present in the map. Syntax: V remove(Object key)Parameters: This method has the only argument key, whose mapping is to be removed from the map.Returns: This method returns the value to which this map previously as
    2 min read
    Map clear() method in Java with Example
    The java.util.Map.clear() method in Java is used to clear and remove all of the elements or mappings from a specified Map collection. Syntax: void clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illustrate
    2 min read
    Map containsKey() method in Java with Examples
    The java.util.Map.containsKey() method is used to check whether a particular key is being mapped into the Map or not. It takes the key element as a parameter and returns True if that element is mapped in the map. Syntax: boolean containsKey(key_element) Parameters: The method takes just one paramete
    2 min read
    Map containsValue() method in Java with Examples
    The java.util.Map.containsValue() method is used to check whether a particular value is being mapped by a single or more than one key in the Map. It takes the value as a parameter and returns True if that value is mapped by any of the key in the map. Syntax: boolean containsValue(Object Value) Param
    2 min read
    Map entrySet() method in Java with Examples
    The java.util.Map.entrySet() method in Java is used to create a set out of the same elements contained in the map. It basically returns a set view of the map or we can create a new set and store the map elements into them. Syntax: map.entrySet() Parameters: The method does not take any parameter. Re
    2 min read
    Java Map equals() Method
    The equals() method of Map interface in Java is used to check if two maps are equal. Two maps are considered equal if they meet the following conditions. Both maps must have the same size.Both maps must contain identical key-value pairs. (It means every key in one map must be associated with the sam
    2 min read
    Map get() method in Java with Examples
    The get() method of Map interface in Java is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key. So, let us check how to get value from the map in Java. Syntax of get() method in JavathisMap.get
    3 min read
    Java Map hashCode() Method
    In Java, the hashCode() method is a part of the Object class and is used to generate a hash code value for an object. Example 1: Hash Code for Different ObjectsThe below Java program demonstrates that every object has a unique hashcode. Java// Java program to demonstrates hashCode() // for different
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences