<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="highlight" name="code">public class LinkedHashSet<;E>; 
extends HashSet<;E>; 
implements Set<;E>;, Cloneable, Serializable 
</pre>
<p>
<b><i>Hash table</i> and <i>linked list</i> implementation of the <i>Set </i>interface</b>, with predictable iteration order. This implementation differs from <b><i>HashSet </i></b>in that it maintains a doubly-linked list running through all of its entries. <b>This linked list defines the iteration ordering,</b> which is the order in which elements were inserted into the set (insertion-order). <b>Note that insertion order is not affected if an element is re-inserted into the set. </b>(An element e is reinserted into a set s if <i><b>s.add(e)</b></i> is invoked when <i><b>s.contains(e)</b></i> would return true immediately prior to the invocation.) </p>
<div align='center' id="ads-id"></div>
<p>This class provides all of the optional Set operations, and permits null elements. Like <b><i>HashSet</i></b>, it provides constant-time performance for the basic operations (add, contains and remove), assuming the hash function disperses elements properly among the buckets. Performance is likely to be just slightly below that of <i><b>HashSet</b></i>, due to the added expense of maintaining the linked list, with one exception: Iteration over a <i><b>LinkedHashSet </b></i>requires time proportional to the size of the set, regardless of its capacity. Iteration over a <i><b>HashSet </b></i>is likely to be more expensive, requiring time proportional to its capacity.</p>
<p> A linked hash set has two parameters that affect its performance: initial capacity and load factor. They are defined precisely as for <i><b>HashSet</b></i>. Note, however, that the penalty for choosing an excessively high value for initial capacity is less severe for this class than for <b><i>HashSet</i></b>, as iteration times for this class are unaffected by capacity.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/05/linkedhashset.jpg" /></div>
<p>
 <b>Note that this implementation is not synchronized.</b> If multiple threads access a linked hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be &#8220;wrapped&#8221; using the <i><b>Collections.synchronizedSet</b></i> method. This is best done at creation time, to prevent accidental <b>unsynchronized </b>access to the set:</p>
<pre class="highlight" name="code">Set s = Collections.synchronizedSet(new LinkedHashSet(...)); 
</pre>
<p><b><i>Constructor </i>Detail:</b><br />
<i><b>LinkedHashSet()</b></i><br />
Constructs a new, empty linked hash set with the default initial capacity (16) and load factor (0.75).<br />
<i><b>LinkedHashSet(Collection c)</b></i><br />
Constructs a new linked hash set with the same elements as the specified collection. Constructs a new linked hash set with the same elements as the specified collection. The linked hash set is created with an initial capacity sufficient to hold the elements in the specified collection and the default load factor (0.75).<br />
<i><b>LinkedHashSet(int initialCapacity)</b></i><br />
Constructs a new, empty linked hash set with the specified initial capacity and the default load factor (0.75).<br />
<i><b>LinkedHashSet(int initialCapacity, float loadFactor)</b></i><br />
Constructs a new, empty linked hash set with the specified initial capacity and load factor. Constructs a new, empty linked hash set with the specified initial capacity and load factor.<br />
<b>Parameters:</b><br />
<i><b>initialCapacity</b></i> &#8211; the initial capacity of the linked hash set<br />
<i><b>loadFactor &#8211;</b></i> the load factor of the linked hash set</p>
<p><i><b>Example:</b></i></p>
<pre class="highlight" name="code">import java.util.*; 
 
public class LinkedHashSetDemo { 
 
 public static void main(String args[]) { 
 // create a hash set 
 LinkedHashSet hs = new LinkedHashSet(); 
 // add elements to the hash set 
 hs.add("B"); 
 hs.add("A"); 
 hs.add("D"); 
 hs.add("E"); 
 hs.add("C"); 
 hs.add("F"); 
 System.out.println(hs); 
 } 
} 
</pre>
<p><i><b>output:</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/05/linkedhashset.png" /></div>
<p><i><b><br />
</b></i> <b>Example of <i>LinkedHashSet </i>class with duplicate value:</b></p>
<pre class="highlight" name="code">import java.util.*; 
 
public class LinkedHashSetDemo { 
 
 public static void main(String args[]) { 
 // create a hash set 
 LinkedHashSet hs = new LinkedHashSet(); 
 // add elements to the hash set 
 hs.add("Dinesh"); 
 hs.add("Anamika"); 
 hs.add("Sweety"); 
 hs.add("Dinesh"); 
 hs.add("Sweety"); 
 hs.add("RAJ"); 
 System.out.println(hs); 
 } 
} 
</pre>
<p><b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/05/linkedhashset2.png" /></div>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<b>Java Collections Tutorial</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/arraylist-class-in-java-collection/">ArrayList class</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedlist-class-in-collection/">LinkedList class</a></b></li>
<li><b><a href="https://dineshonjava.com/listiterator-interface-in-collection/">ListIterator interface</a></b></li>
<li><b><a href="https://dineshonjava.com/hashset-class-in-collection/">HashSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/treeset-classin-collection/">TreeSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-hashset-and-treeset/">Difference between TreeSet &; HashSet</a></b></li>
<li><b><a href="https://dineshonjava.com/map-interface-in-collection/">Map interface</a></b></li>
<li><b><a href="https://dineshonjava.com/hashmap-class-in-collection-framework/">HashMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/how-does-java-hashmap-work-internally/">How does work HashMap?</a> </b></li>
<li><b><a href="https://dineshonjava.com/difference-between-treemap-vs-hashmap/">Difference between TreeMap vs HashMap</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedhashmap-class-in-collection/">LinkedHashMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/treemap-class-in-collection-framework/">TreeMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/hashtable-class-in-collection-framework/">Hashtable class</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-hashmap-and/">Difference between HashMap and HashTable in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/sorting-in-collection-framework/">Sorting</a></b></li>
<li><b><a href="https://dineshonjava.com/java-comparable-and-comparator/">Comparable interface</a></b></li>
</ol>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/hashset-class-in-collection/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/treeset-classin-collection/">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/hashset-class-in-collection/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/treeset-classin-collection/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '463'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…