Previously we have discussed about
Here we learn what is LinkedHashSet In Java.But before that we must know, What is Collection Interface in Java ?
Class LinkedHashSet<E>
Type Parameters:
E - the type of elements maintained by this set
All Implemented Interfaces:
Declaration of LinkedHashSet:
public class LinkedHashSet<E>
extends HashSet<E>
LinkedHashSet maintains a LinkedList(means it maintains a doubly-linked list ) of the entries in the set, the order in which they were inserted. This class provides all of the optional Set operations, and permits null elements. Note that insertion order is not affected if an element is re-inserted into the set.
A LinkedHashSet has two parameters that affect its performance: initial capacity and load factor. They are defined precisely as for HashSet.
LinkedHashSet is an example of fail-fast iterator, means if the set is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException.
LinkedHashSet implementation is not synchronized. If multiple threads access a linked hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally, to prevent accidental unsynchronized access to the set ,we can synchronized by using:
Set s = Collections.synchronizedSet(new LinkedHashSet(...));
This class is a member of the Java Collections Framework.
Constructor Summary of LinkedHashSet:
Constructor
|
Description
|
LinkedHashSet()
|
Constructs a new, empty linked hash set with the default initial
capacity (16) and load factor (0.75).
|
LinkedHashSet(Collection<? extends E> c)
|
Constructs a new linked hash set with the same elements as the
specified collection.
|
LinkedHashSet(int initialCapacity)
|
Constructs a new, empty linked hash set with the specified initial
capacity and the default load factor (0.75).
|
LinkedHashSet(int initialCapacity, float loadFactor)
|
Constructs a new, empty linked hash set with the specified initial
capacity and load factor.
|
Example of LinkedHashSet:
import java.util.Iterator;
import java.util.LinkedHashSet;
public class LinkedhashSetClassExample {
public static void main(String[] args) {
LinkedHashSet<Integer> ll = new LinkedHashSet<Integer>();
ll.add(5);
ll.add(2);
ll.add(3);
ll.add(8);
System.out.println("LinkedHashSet Size : "+ll.size());
System.out.println("LinkedHashSet Elements : "+ll);
System.out.println("\nIteration over LinkedHashSet : ");
Iterator<Integer> it = ll.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println("\nIteration Using Lambda Expression : ");
ll.forEach( i -> System.out.println(i));
}
}
Program Output:
LinkedHashSet Size : 4
LinkedHashSet Elements : [5, 2, 3, 8]
Iteration over LinkedHashSet :
5
2
3
8
Iteration Using Lambda Expression :
5
2
3
8
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.


No comments:
Post a Comment