CodesJava

Easy learning with example program codes

how to remove all elements from linkedhashset in java?


LinkedHashSet:

LinkedHashSet extends HashSet and implements the Set interface. It maintains insertion order for its elements.

We can use clear() method to remove all elements from linkedhashset in java.

Example:

package com.codesjava;
 
import java.util.LinkedHashSet;
 
public class Test {
  public static void main(String args[]){
	//Create LinkedHashSet
	LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();
        //Add elements to LinkedHashSet
	linkedHashSet.add("Jai");
	linkedHashSet.add("Mahesh");
	linkedHashSet.add("Vikas");
	linkedHashSet.add("Sahil");
	linkedHashSet.add("Sachin");
	linkedHashSet.add("Vijay");
	System.out.println("LinkedHashSet: " + linkedHashSet);
	linkedHashSet.clear();
        System.out.println("LinkedHashSet after modification: " + linkedHashSet);
  }
}

Output

LinkedHashSet: [Jai, Mahesh, Vikas, Sahil, Sachin, Vijay]
LinkedHashSet after modification: []
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki