Remove all values from Java LinkedHashMap

Java 8Object Oriented ProgrammingProgramming

Use the clear() method to remove all the values from LinkedHashMap in Java.

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 us remove all the values −

l.clear();

The following is an example to remove all values from LinkedHashMap −

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);
      l.clear();
      System.out.println("\nLinkedHashMap elements now...\n"+l);
   }
}

Output

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

LinkedHashMap elements now...
{}
raja
Published on 11-Jan-2019 06:15:37
Advertisements