• September 1, 2014
  • Core Java
  • Comments Off on LinkedHashSet in java

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 javawithease
 */
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.

Related Topics:

Collection framework in java.
Collection interfaces in java.
Collection interface in java with example.
Set interface in java with example.
SortedSet interface in java with example.
List interface in java with example.
Map interface in java with example.
Daemon thread in java in java with example.
SortedMap interface in java with example.
Queue interface in java with example.
Deque interface in java with example.
Enumeration interface in java with example.
Collection classes in java with example.
HashSet in java with example.
LinkedHashSet in java with example.
TreeSet in java with example.
ArrayList in java with example.
LinkedList in java with example.
HashMap in java with example.
LinkedHashMap in java with example.
TreeMap in java with example.
PriorityQueue in java with example.
ArrayDeque in java with example.
Abstract classes in collection framework in java with example.
Sorting in java with example.
Comparable interface in java with example.
Comparator interface in java with example.
Properties class in java with example.
Hashtable in java with example.
ListIterator interface in java with example.

 


Comments are closed.