Java Collections

LinkedHashSet Class in Collection

<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;" trbidi&equals;"on">&NewLine;<pre class&equals;"highlight" name&equals;"code">public class LinkedHashSet&lt&semi;E&gt&semi; &NewLine;extends HashSet&lt&semi;E&gt&semi; &NewLine;implements Set&lt&semi;E&gt&semi;&comma; Cloneable&comma; Serializable &NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b><i>Hash table<&sol;i> and <i>linked list<&sol;i> implementation of the <i>Set <&sol;i>interface<&sol;b>&comma; with predictable iteration order&period; This implementation differs from <b><i>HashSet <&sol;i><&sol;b>in that it maintains a doubly-linked list running through all of its entries&period; <b>This linked list defines the iteration ordering&comma;<&sol;b> which is the order in which elements were inserted into the set &lpar;insertion-order&rpar;&period; <b>Note that insertion order is not affected if an element is re-inserted into the set&period; <&sol;b>&lpar;An element e is reinserted into a set s if <i><b>s&period;add&lpar;e&rpar;<&sol;b><&sol;i> is invoked when <i><b>s&period;contains&lpar;e&rpar;<&sol;b><&sol;i> would return true immediately prior to the invocation&period;&rpar; <&sol;p>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<p>This class provides all of the optional Set operations&comma; and permits null elements&period; Like <b><i>HashSet<&sol;i><&sol;b>&comma; it provides constant-time performance for the basic operations &lpar;add&comma; contains and remove&rpar;&comma; assuming the hash function disperses elements properly among the buckets&period; Performance is likely to be just slightly below that of <i><b>HashSet<&sol;b><&sol;i>&comma; due to the added expense of maintaining the linked list&comma; with one exception&colon; Iteration over a <i><b>LinkedHashSet <&sol;b><&sol;i>requires time proportional to the size of the set&comma; regardless of its capacity&period; Iteration over a <i><b>HashSet <&sol;b><&sol;i>is likely to be more expensive&comma; requiring time proportional to its capacity&period;<&sol;p>&NewLine;<p> A linked hash set has two parameters that affect its performance&colon; initial capacity and load factor&period; They are defined precisely as for <i><b>HashSet<&sol;b><&sol;i>&period; Note&comma; however&comma; that the penalty for choosing an excessively high value for initial capacity is less severe for this class than for <b><i>HashSet<&sol;i><&sol;b>&comma; as iteration times for this class are unaffected by capacity&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;linkedhashset&period;jpg" &sol;><&sol;div>&NewLine;<p>&NewLine; <b>Note that this implementation is not synchronized&period;<&sol;b> If multiple threads access a linked hash set concurrently&comma; and at least one of the threads modifies the set&comma; it must be synchronized externally&period; This is typically accomplished by synchronizing on some object that naturally encapsulates the set&period; If no such object exists&comma; the set should be &&num;8220&semi;wrapped&&num;8221&semi; using the <i><b>Collections&period;synchronizedSet<&sol;b><&sol;i> method&period; This is best done at creation time&comma; to prevent accidental <b>unsynchronized <&sol;b>access to the set&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">Set s &equals; Collections&period;synchronizedSet&lpar;new LinkedHashSet&lpar;&period;&period;&period;&rpar;&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<p><b><i>Constructor <&sol;i>Detail&colon;<&sol;b><br &sol;>&NewLine;<i><b>LinkedHashSet&lpar;&rpar;<&sol;b><&sol;i><br &sol;>&NewLine;Constructs a new&comma; empty linked hash set with the default initial capacity &lpar;16&rpar; and load factor &lpar;0&period;75&rpar;&period;<br &sol;>&NewLine;<i><b>LinkedHashSet&lpar;Collection c&rpar;<&sol;b><&sol;i><br &sol;>&NewLine;Constructs a new linked hash set with the same elements as the specified collection&period; Constructs a new linked hash set with the same elements as the specified collection&period; The linked hash set is created with an initial capacity sufficient to hold the elements in the specified collection and the default load factor &lpar;0&period;75&rpar;&period;<br &sol;>&NewLine;<i><b>LinkedHashSet&lpar;int initialCapacity&rpar;<&sol;b><&sol;i><br &sol;>&NewLine;Constructs a new&comma; empty linked hash set with the specified initial capacity and the default load factor &lpar;0&period;75&rpar;&period;<br &sol;>&NewLine;<i><b>LinkedHashSet&lpar;int initialCapacity&comma; float loadFactor&rpar;<&sol;b><&sol;i><br &sol;>&NewLine;Constructs a new&comma; empty linked hash set with the specified initial capacity and load factor&period; Constructs a new&comma; empty linked hash set with the specified initial capacity and load factor&period;<br &sol;>&NewLine;<b>Parameters&colon;<&sol;b><br &sol;>&NewLine;<i><b>initialCapacity<&sol;b><&sol;i> &&num;8211&semi; the initial capacity of the linked hash set<br &sol;>&NewLine;<i><b>loadFactor &&num;8211&semi;<&sol;b><&sol;i> the load factor of the linked hash set<&sol;p>&NewLine;<p><i><b>Example&colon;<&sol;b><&sol;i><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">import java&period;util&period;&ast;&semi; &NewLine; &NewLine;public class LinkedHashSetDemo &lbrace; &NewLine; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar; &lbrace; &NewLine; &sol;&sol; create a hash set &NewLine; LinkedHashSet hs &equals; new LinkedHashSet&lpar;&rpar;&semi; &NewLine; &sol;&sol; add elements to the hash set &NewLine; hs&period;add&lpar;"B"&rpar;&semi; &NewLine; hs&period;add&lpar;"A"&rpar;&semi; &NewLine; hs&period;add&lpar;"D"&rpar;&semi; &NewLine; hs&period;add&lpar;"E"&rpar;&semi; &NewLine; hs&period;add&lpar;"C"&rpar;&semi; &NewLine; hs&period;add&lpar;"F"&rpar;&semi; &NewLine; System&period;out&period;println&lpar;hs&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><i><b>output&colon;<&sol;b><&sol;i><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;linkedhashset&period;png" &sol;><&sol;div>&NewLine;<p><i><b><br &sol;>&NewLine;<&sol;b><&sol;i> <b>Example of <i>LinkedHashSet <&sol;i>class with duplicate value&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">import java&period;util&period;&ast;&semi; &NewLine; &NewLine;public class LinkedHashSetDemo &lbrace; &NewLine; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar; &lbrace; &NewLine; &sol;&sol; create a hash set &NewLine; LinkedHashSet hs &equals; new LinkedHashSet&lpar;&rpar;&semi; &NewLine; &sol;&sol; add elements to the hash set &NewLine; hs&period;add&lpar;"Dinesh"&rpar;&semi; &NewLine; hs&period;add&lpar;"Anamika"&rpar;&semi; &NewLine; hs&period;add&lpar;"Sweety"&rpar;&semi; &NewLine; hs&period;add&lpar;"Dinesh"&rpar;&semi; &NewLine; hs&period;add&lpar;"Sweety"&rpar;&semi; &NewLine; hs&period;add&lpar;"RAJ"&rpar;&semi; &NewLine; System&period;out&period;println&lpar;hs&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>output&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;linkedhashset2&period;png" &sol;><&sol;div>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px&semi;">&NewLine;<b>Java Collections Tutorial<&sol;b><&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;arraylist-class-in-java-collection&sol;">ArrayList class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedlist-class-in-collection&sol;">LinkedList class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;listiterator-interface-in-collection&sol;">ListIterator interface<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;">HashSet class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;">TreeSet class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-hashset-and-treeset&sol;">Difference between TreeSet &amp&semi; HashSet<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;map-interface-in-collection&sol;">Map interface<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashmap-class-in-collection-framework&sol;">HashMap class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;how-does-java-hashmap-work-internally&sol;">How does work HashMap&quest;<&sol;a> <&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-treemap-vs-hashmap&sol;">Difference between TreeMap vs HashMap<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedhashmap-class-in-collection&sol;">LinkedHashMap class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treemap-class-in-collection-framework&sol;">TreeMap class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashtable-class-in-collection-framework&sol;">Hashtable class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-hashmap-and&sol;">Difference between HashMap and HashTable in Java<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;sorting-in-collection-framework&sol;">Sorting<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-comparable-and-comparator&sol;">Comparable interface<&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;div>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '463'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago