beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java – Remove mapping from HashMap example

By Chaitanya Singh | Filed Under: Java Collections

Example

In this example we are gonna see how to remove a specific mapping from HashMap using the key value of Key-value pair. We will be using the following method of HashMap class to perform this operation:

public Value remove(Object key): Removes the mapping for the specified key from this map if present and returns the Element value for that particular Key. More about remove method from Javadoc.

Complete Code:

import java.util.HashMap;

public class RemoveMappingExample {
 
 public static void main(String[] args) {
 
    // Creating a HashMap of int keys and String values
    HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
 
    // Adding Key and Value pairs to HashMap
    hashmap.put(11,"Value1");
    hashmap.put(22,"Value2");
    hashmap.put(33,"Value3");
    hashmap.put(44,"Value4");
    hashmap.put(55,"Value5");
    hashmap.put(66,"Value6");
 
    // Displaying HashMap Elements
    System.out.println("HashMap Elements: " + hashmap);

    // Removing Key-Value pairs for key 33
    Object removedElement1 = hashmap.remove(33);
    System.out.println("Element removed is: " +removedElement1);
 
    // Removing Key-Value pairs for key 55
    Object removedElement2 = hashmap.remove(55);
    System.out.println("Element removed is: " +removedElement2);

    // Displaying HashMap Elements after remove
    System.out.println("After Remove:");
    System.out.println("--------------");
    System.out.println("HashMap Elements: " + hashmap);
 }
}

Output:

HashMap Elements: {33=Value3, 55=Value5, 66=Value6, 22=Value2, 11=Value1, 44=Value4}
Element removed is: Value3
Element removed is: Value5
After Remove:
--------------
HashMap Elements: {66=Value6, 22=Value2, 11=Value1, 44=Value4}

Enjoyed this post? Try these related posts

  1. Remove all mappings from TreeMap example – Java
  2. Get size of Hashtable example in Java
  3. How to convert ArrayList to string array in java
  4. How to synchronize HashMap in Java with example
  5. How to sort ArrayList in descending order in Java
  6. How to convert LinkedList to array using toArray() in Java

Leave a Reply Cancel reply

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

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap