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.



Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap