Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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:
AbstractCollection remove() Method in Java with Examples
Next article icon

AbstractMap remove() Method in Java with Examples

Last Updated : 26 Oct, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report

The AbstractMap.remove() is an inbuilt method of AbstractMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map.

Syntax:

AbstractMap.remove(Object key)

Parameters: The method takes one parameter key whose mapping is to be removed from the Map.

Return Value: The method returns the value that was previously mapped to the specified key if the key exists else the method returns NULL.

Below programs illustrates the working of AbstractMap.remove() method:

Program 1: When passing an existing key.




// Java code to illustrate the remove() method
  
import java.util.*;
  
public class Abstract_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty AbstractMap
        AbstractMap<Integer, String>
            abs_map = new HashMap<Integer, String>();
  
        // Mapping string values to int keys
        abs_map.put(10, "Geeks");
        abs_map.put(15, "4");
        abs_map.put(20, "Geeks");
        abs_map.put(25, "Welcomes");
        abs_map.put(30, "You");
  
        // Displaying the AbstractMap
        System.out.println("Initial Mappings are: "
                           + abs_map);
  
        // Removing the existing key mapping
        String
            returned_value
            = (String)abs_map.remove(20);
  
        // Verifying the returned value
        System.out.println("Returned value is: "
                           + returned_value);
  
        // Displayin the new map
        System.out.println("New map is: "
                           + abs_map);
    }
}
 
 
Output:
  Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}  Returned value is: Geeks  New map is: {25=Welcomes, 10=Geeks, 30=You, 15=4}  

Program 2: When passing a new key.




// Java code to illustrate the remove() method
  
import java.util.*;
  
public class Abstract_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty AbstractMap
        AbstractMap<Integer, String>
            abs_map = new HashMap<Integer, String>();
  
        // Mapping string values to int keys
        abs_map.put(10, "Geeks");
        abs_map.put(15, "4");
        abs_map.put(20, "Geeks");
        abs_map.put(25, "Welcomes");
        abs_map.put(30, "You");
  
        // Displaying the AbstractMap
        System.out.println("Initial Mappings are: "
                           + abs_map);
  
        // Removing the new key mapping
        String
            returned_value
            = (String)abs_map.remove(50);
  
        // Verifying the returned value
        System.out.println("Returned value is: "
                           + returned_value);
  
        // Displayin the new map
        System.out.println("New map is: " + abs_map);
    }
}
 
 
Output:
  Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}  Returned value is: null  New map is: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}  

Note: The same operation can be performed with any type of Mappings with variation and combination of different data types.



Next Article
AbstractCollection remove() Method in Java with Examples
author
chinmoy lenka
Improve
Article Tags :
  • Java
  • Java - util package
  • Java-AbstractMap
  • Java-Collections
  • Java-Functions
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • AbstractList remove() Method in Java with Examples
    The remove(int index) method of java.util.AbstractList class is used to remove an element from an abstract list from a specific position or index. Syntax: AbstractList.remove(int index) Parameters: The parameter index is of integer data type and specifies the position of the element to be removed fr
    2 min read
  • AbstractQueue remove() method in Java with examples
    The remove() method of AbstractQueue returns and removes the head of this queue. Syntax: public E remove() Parameters: This method does not accept any parameters. Returns: The method returns the head of the Queue. Exception: The function throws an NoSuchElementException if the queue is empty. Below
    2 min read
  • AbstractMap put() Method in Java with Examples
    The AbstractMap put() method inserts a mapping into an AbstractMap. This means we can add a specific key and its value, into a particular map. If an existing key is passed, then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole. Exampl
    3 min read
  • AbstractCollection remove() Method in Java with Examples
    The remove(Object O) method of Java AbstractCollection is to remove a particular element from a Collection. Syntax: AbstractCollection.remove(Object O) Parameters: The parameter O is of the type of Collection and specifies the element to be removed from the collection. Return Value: This method retu
    2 min read
  • AbstractSequentialList remove() method in Java with Examples
    The remove(int index) method of AbstractSequentialList is used to remove an element from an abstract sequential list from a specific position or index. Syntax: AbstractSequentialList.remove(int index) Parameters: The parameter index is of integer data type and specifies the position of the element t
    2 min read
  • AbstractMap clear() Method in Java with Examples
    The AbstractMap.clear() method in Java is used to clear and remove all of the elements or mappings from a specified Map. Syntax: AbstractMap.clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illustrate the wo
    2 min read
  • ArrayList removeIf() Method in Java with Examples
    The Java ArrayList removeIf() method is used to remove all elements from the ArrayList that satisfy a given predicate filter. The predicate is passed as a parameter to the method, and any runtime exceptions thrown during iteration or by the predicate are passed to the caller. The removeIf() method u
    2 min read
  • AbstractList set() Method in Java with Examples
    The set() method of java.util.AbstractList class is used to replace any particular element in the abstract list created using the AbstractList class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() metho
    2 min read
  • ArrayList removeAll() Method in Java with Examples
    The removeAll() method of the ArrayList class in Java is used to remove all elements of an ArrayList that are specified in another collection or within the same list itself. Example 1: Here, we use the removeAll() method to remove all elements from an ArrayList by passing the same list as an argumen
    3 min read
  • CompositeName remove() method in Java with Examples
    The remove() method of a javax.naming.CompositeName class is used to remove a component situated at position posn from this composite name. The position posn is passed as a parameter to this method. This method removes the component of this composite name object at position 'posn' and components pre
    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