LinkedHashMap

Remove mapping from LinkedHashMap example

With this example we are going to demonstrate how to remove mapping from a LinkedHashMap, that is removing a key value pair from a LinkedHashMap. In short, to remove mapping from a LinkedHashMap you should:

  • Create a new LinkedHashMap.
  • Populate the linkedHashMap with elements, with put(K key, V value) API method of LinkedHashMap.
  • Invoke remove(Object key) API method of LinkedHashMap. It removes the mapping for the specified key from this map if present, and returns the previous value associated with this key, or null if there was no mapping for the key.

Let’s take a look at the code snippet that follows:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.javacodegeeks.snippets.core;
  
import java.util.LinkedHashMap;
  
public class RemoveMappingLinkedHashMap {
  
  public static void main(String[] args) {
  
 
// Create a LinkedHashMap and populate it with elements
 
LinkedHashMap linkedHashMap = new LinkedHashMap();
 
linkedHashMap.put("key_1","value_1");
 
linkedHashMap.put("key_2","value_2");
 
linkedHashMap.put("key_3","value_3");
 
 
System.out.println("LinkedhashMap contains : " + linkedHashMap);
 
 
/*
 
  Object remove(Object key) operantion removes a key value pair from LinkedHashMap.
 
  It returns either the value mapped with the key previously or null if no value was mapped.    
 
*/
 
Object value = linkedHashMap.remove("key_2");
 
 
System.out.println("After removing value : " + value + " LinkedhashMap contains : " + linkedHashMap);
     
  }
}

Output:

LinkedhashMap contains : {key_1=value_1, key_2=value_2, key_3=value_3}
After removing value : value_2, LinkedhashMap contains : {key_1=value_1, key_3=value_3}

 
This was an example of how to remove mapping from a LinkedHashMap in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button