CodesJava

Easy learning with example program codes

LinkedHashSet in java


LinkedHashSet:

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

LinkedHashSet example:

LinkedHashSetTest.java

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
 
/**
 * This class is used to show the LinkedHashSet functionality.
 * @author codesjava
 */
public class LinkedHashSetTest {
	public static void main(String args[]){
		//Create LinkedHashSet object.
		Set linkedHashSet = new LinkedHashSet();
 
		//Add objects to the LinkedHashSet.
		linkedHashSet.add("Roxy");
		linkedHashSet.add("Sunil");
		linkedHashSet.add("Sandy");
		linkedHashSet.add("Munish");
		linkedHashSet.add("Pardeep");
 
		//Print the LinkedHashSet object.
		System.out.println("LinkedHashSet elements:");
		System.out.println(linkedHashSet);
 
		//Print the LinkedHashSet elements using iterator.
		Iterator iterator=linkedHashSet.iterator();
		System.out.println("LinkedHashSet elements " +
				"using iterator:");
		while(iterator.hasNext()){  
		   System.out.println(iterator.next());  
		}  
	}
}

Output:

LinkedHashSet elements:
[Roxy, Sunil, Sandy, Munish, Pardeep]
LinkedHashSet elements using iterator:
Roxy
Sunil
Sandy
Munish
Pardeep

Download this example.
 
Next Topic: TreeSet in java with example.
Previous Topic: HashSet in java with example.

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