Remove a value from Java LinkedHashMap

Java 8Object Oriented ProgrammingProgramming

Use the remove() method to remove a single value from LinkedHashMap −

At first, create a LinkedHashMap and add some elements −

LinkedHashMap<String,String> l = new LinkedHashMap<String,String>();
l.put("1", "Jack");
l.put("2", "Tom");
l.put("3", "Jimmy");
l.put("4", "Morgan");
l.put("5", "Tim");
l.put("6", "Brad");

Now, let’s say you need to remove the element 2 from the LinkedHashMap. For that, use the remove() method −

Object ob = l.remove("2");

The following is an example to remove a value from LinkedHashMap in Java −

Example

 Live Demo

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      LinkedHashMap<String,String> l = new LinkedHashMap<String,String>();
      l.put("1", "Jack");
      l.put("2", "Tom");
      l.put("3", "Jimmy");
      l.put("4", "Morgan");
      l.put("5", "Tim");
      l.put("6", "Brad");
      System.out.println("LinkedHashMap elements...\n"+l);
      // Remove an element
      Object ob = l.remove("2");
      System.out.println("\nLinkedHashMap elements after removing an element...\n"+l);
   }
}

Output

LinkedHashMap elements...
{1=Jack, 2=Tom, 3=Jimmy, 4=Morgan, 5=Tim, 6=Brad}

LinkedHashMap elements after removing an element...
{1=Jack, 3=Jimmy, 4=Morgan, 5=Tim, 6=Brad}
raja
Published on 11-Jan-2019 10:37:34
Advertisements