DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Floyd's Cycle Algorithm for Fraud Detection in Java Systems
  • Merge Multiple PDFs in MuleSoft
  • Scaling Java Microservices to Extreme Performance Using NCache
  • What Is a Jagged Array in Java With Examples?

Trending

  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • Google Cloud Document AI Basics
  • Emerging Data Architectures: The Future of Data Management
  • *You* Can Shape Trend Reports: Join DZone's Software Supply Chain Security Research
  1. DZone
  2. Data Engineering
  3. Data
  4. How to Remove Key Value Pairs or Entries from HashMap [Snippet]

How to Remove Key Value Pairs or Entries from HashMap [Snippet]

Let's take a look at how to remove entries from a map in Java, complete with examples.

By 
Ramesh Fadatare user avatar
Ramesh Fadatare
·
Updated Dec. 20, 18 · Code Snippet
Likes (5)
Comment
Save
Tweet
Share
83.4K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will discuss how to remove key-value pair or entry from HashMap. In this article, we will look into two different ways we can remove key-value pair or entry from HashMap.

  1. Using java.util.Map.remove(Object key) method
  2. Using java.util.Collection.removeIf(Predicate<? super Entry<String, Integer>  >filter)  method from Java 8

1. Using java.util.Map.remove(Object key) Method

The remove() method removes the mapping for a key from this map if it is present (optional operation).

package net.javaguides.examples;

import java.util.HashMap;
import java.util.Map;

/**
 * Demonstrates How to Remove Key Value Pairs or Entries from HashMap [Snippet]
 * @author Ramesh Fadatare
 *
 */
public class RemoveKeyValuePairHashMap {

    public static void main(String[] args) {

        // create two hashmap
        Map < String, Integer > courseMap = new HashMap < String, Integer > ();

        courseMap.put("Java", 1);
        courseMap.put("C", 2);
        courseMap.put("C++", 3);

        System.out.println("Before removal");
        printMap(courseMap);

        // using normal remove method
        courseMap.remove("Java");

        System.out.println("After removal");
        printMap(courseMap);
    }

    private static void printMap(Map < String, Integer > courseMap) {
        for (String s: courseMap.keySet()) {
            System.out.println(s);
        }
    }
}


Output:

Before removal
Java
C++
C
After removal
C++
C


2. java.util.Collection.removeIf(Predicate<? super Entry<String, Integer>> filter) Method From Java 8

The removeIf() method of the Collection class, which allows you to remove entries based upon some condition or predicate, takes a lambda expression, which can be used to supply the predicate you want. For example, we can rewrite the above code in Java 8, as shown in the following example:

package net.javaguides.examples;

import java.util.HashMap;
import java.util.Map;

/**
 * Demonstrates How to Remove Key Value Pairs or Entries from HashMap [Snippet]
 * @author Ramesh Fadatare
 *
 */
public class RemoveKeyValuePairHashMap {

    public static void main(String[] args) {

        // create two hashmap
        Map < String, Integer > courseMap = new HashMap < String, Integer > ();

        courseMap.put("Java", 1);
        courseMap.put("C", 2);
        courseMap.put("C++", 3);

        System.out.println("Before removal : using java 8 ");
        printMap(courseMap);

        // using java 8 removeIf method
        courseMap.entrySet().removeIf(e - > e.getKey().equals("Java"));

        System.out.println("After removal : using Java 8");
        printMap(courseMap);

    }

    private static void printMap(Map < String, Integer > courseMap) {
        for (String s: courseMap.keySet()) {
            System.out.println(s);
        }
    }
}


Output:

Output:
Before removal : using java 8 
Java
C++
C
After removal : using Java 8
C++
C


Collections framework examples:

  • Conversion Between Array and Set in Java
  • Conversion Between Array and List in Java
  • Java Convert Map to Set Example
  • Java Convert Map to List Example
  • Java Convert Map to Array Example
  • Convert a Map to an Array, List and Set in Java
  • Java 8 Convert List to Map Example
  • Java 8 - Merging Two Maps Example
  • Java Convert Array to String [Snippet]
  • Different Ways to Iterate over List, Set and Map in Java
  • Java Comparator Interface Example
  • Java Comparable Interface Example
  • Java IdentityHashMap Example
  • Java WeakHashMap Example
  • Java EnumMap Example
  • Java CopyOnWriteArraySet Example
  • Java EnumSet Class Example
  • Guide to Java 8 forEach Method
  • Different Ways to Iterate over a List in Java [Snippet]
  • Different Ways to Iterate over a Set in Java [Snippet]
  • Different Ways to Iterate over a Map in Java [Snippet]
Data structure Java (programming language)

Published at DZone with permission of Ramesh Fadatare. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Floyd's Cycle Algorithm for Fraud Detection in Java Systems
  • Merge Multiple PDFs in MuleSoft
  • Scaling Java Microservices to Extreme Performance Using NCache
  • What Is a Jagged Array in Java With Examples?

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: