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

Remove mapping from Hashtable example – Java

By Chaitanya Singh | Filed Under: Java Collections

In this tutorial we are gonna see how to remove a key-value mapping from Hashtable. We will be using remove(Object key) method of Hashtable class.

Example

Method used in the below program is:

public V remove(Object key): Removes the key (and its corresponding value) from this hashtable. This method does nothing if the key is not in the hashtable.

import java.util.Hashtable;

public class RemoveMappingExample {
 
 public static void main(String[] args) {
 
    // Creating a Hashtable
    Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>();
 
    // Adding Key and Value pairs to Hashtable
    hashtable.put(11,"AA");
    hashtable.put(22,"BB");
    hashtable.put(33,"CC");
    hashtable.put(44,"DD");
    hashtable.put(55,"EE");
 
    //Before remove
    System.out.println("Hashtable contains:" + hashtable);
 
    // Removing key-value pairs for key 44
    Object removedValue = hashtable.remove(44);
 
    //After remove
    System.out.println("After remove:");
    System.out.println("Hashtable Key-value pairs: " + hashtable);
 }
}

Output:

Hashtable contains:{55=EE, 44=DD, 33=CC, 22=BB, 11=AA}
After remove:
Hashtable Key-value pairs: {55=EE, 33=CC, 22=BB, 11=AA}

In the above program we have provided the key 44 while calling remove(key) method so the key-value mapping corresponding to key 44 has been removed from Hashtable and the method returned the value mapped to the key 44.

Enjoyed this post? Try these related posts

  1. Java ArrayList add() Method Example
  2. How to convert ArrayList to string array in java
  3. How to copy and add all list elements to ArrayList in Java
  4. How to Set Vector size example
  5. Vector Enumeration example in Java
  6. Check key & Value existence in Hashtable example – 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