Remove specified element from Java LinkedHashSet

Java 8Object Oriented ProgrammingProgramming

To remove a specified element from LinkedHashSet, use the remove() and include the element you want to remove as a parameter.

First, set LinkedHashSet and add elements −

LinkedHashSet<Integer> hashSet = new LinkedHashSet<Integer>();
hashSet.add(10);
hashSet.add(20);
hashSet.add(30);
hashSet.add(40);
hashSet.add(50);
hashSet.add(60);

Let us now remove an element −

hashSet.remove(10);

The following is an example to remove specified element from LinkedHashSet −

Example

 Live Demo

import java.util.LinkedHashSet;
public class Demo {
   public static void main(String[] args) {
      LinkedHashSet<Integer> hashSet = new LinkedHashSet<Integer>();
      hashSet.add(10);
      hashSet.add(20);
      hashSet.add(30);
      hashSet.add(40);
      hashSet.add(50);
      hashSet.add(60);
      System.out.println("LinkedHashSet...");
      System.out.println(hashSet);
      hashSet.remove(10);
      System.out.println("\nUpdated LinkedHashSet...");
      System.out.println(hashSet);
   }
}

Output

LinkedHashSet...
[10, 20, 30, 40, 50, 60]

Updated LinkedHashSet...
[20, 30, 40, 50, 60]
raja
Published on 11-Jan-2019 05:31:38
Advertisements