Java LinkedHashSet
LinkedHashSet in Java with Example
Java LinkedHashSet extends HashSet and Implements the Set interface. It doesn’t contain only duplicate values like HashSet. It also permits the null elements. It maintains the order of insertion. It returns the elements in the order in which they are inserted. It also maintains the linked list of the elements in the set. The syntax of the LinkedHashSet is:LinkedHashSet<String> hs= new LinkedHashSet<String> ();It also has two factors which affect the performance of LinkedHashSet-> Load Factor and Initial Capacity. Difference between HashSet and LinkedHashSet:
Categories |
HashSet |
LinkedHashSet |
Internal Working | Internally uses HashMap for storing objects. | Internally uses LinkedHashMap for storing objects. |
Order | Not maintain the insertion order. | Maintain the insertion order. |
Complexity | The complexity is O(1). | The complexity is O(1). |
Performance | Its performance is better than LinkedHashSet. | Its performance is slower than HashSet due to the insertion order. |
Compare | It uses equals () and hashCode (). | It also uses equals () and hashCode () for comparing. |
Null Elements | It allows only one null element. | It allows only one null element. |
When to use | If you want unique elements but don’t want to maintain the order of insertion. | It used when to want to maintain the insertion order of the elements. |
Syntax | HashSet<String> hs= new HashSet<String> (); | LinkedHashSet<String> h= new LinkedHashSet<String> (); |
- The HashSet and LinkedHashSet implements Set interface. Hence, they are not allowed to keep the duplicate value.
- HashSet and LinkedHashSet both are cloneable and Serializable.
- You have to make it externally synchronize if you want to use multi-threading environment because both do not thread safe.
Time Complexity of LinkedHashSet
LinkedHashSet uses the HashTable Data Structure. The Complexity of the LinkedHashSet is O(1) time for adding, removing, and retrieving operations.Constructors of LinkedHashSet
- LinkedHashSet (): It is used to create the default HashSet.
- LinkedHashSet (Collection c): It is used to initialize with elements of the collection.
- LinkedHashSet (int size): It is used to initialize the size of LinkedHashSet.
- LinkedHashSet (Int Capacity, float Loadfactor): This is used to initialize both the capacity and load factor.
- add (object o): This method is used to add the elements in HashSet.
import java.util. LinkedHashSet; public class AddExample { public static void main (String args []) { LinkedHashSet<Integer> lh= new LinkedHashSet<Integer> (); lh.add (99); lh.add (7); lh.add (0); lh.add (67); lh.add (66); System.out.println (lh); } }Output:
[99, 7, 0, 67, 66]
- remove (object o): Used to remove the elements from the set.
import java.util. LinkedHashSet; public class RemoveExample { public static void main (String args []) { LinkedHashSet<Integer> lh= new LinkedHashSet<Integer> (); lh.add (99); lh.add (7); lh.add (0); lh.add (67); lh.add (66); System.out.println (lh); lh.remove (0); System.out.println ("After removing: "+ lh); } }Output:
[99, 7, 0, 67, 66]After removing:
[99, 7, 67, 66]
- contains (object o): It returns true if the element found in the set.
import java.util. LinkedHashSet; public class ContainExample { public static void main (String args []) { LinkedHashSet<Integer> lh= new LinkedHashSet<Integer> (); lh.add (99); lh.add (7); lh.add (0); lh.add (67); lh.add (66); System.out.println (lh); System.out.println ("Does the set contain 67?"+ lh.contains (67)); System.out.println ("Does the set contain 99?”+ lh.contains (99)); System.out.println ("Does the set contain 88?”+ lh.contains (88)); } }Output:
[99, 7, 0, 67, 66]Does the set contain 67? true Does the set contain 99? true Does the set contain 88? false
- isEmpty (): It returns true if the set is empty otherwise false.
import java.util. LinkedHashSet; public class EmptyExample { public static void main (String args []) { LinkedHashSet<Integer> lh= new LinkedHashSet<Integer> (); lh.add (99); lh.add (7); lh.add (0); lh.add (67); lh.add (66); System.out.println (lh); System.out.println ("Is the set empty: " + lh.isEmpty ()); lh.clear (); System.out.println ("Is the set empty: " + lh.isEmpty ()); } }Output:
[99, 7, 0, 67, 66]Is the set empty: false Is the set empty: true
- int size (): This method is used to return the number of elements in the set.
import java.util. LinkedHashSet; public class SizeExample { public static void main (String args []) { LinkedHashSet<Integer> lh= new LinkedHashSet<Integer> (); lh.add (99); lh.add (7); lh.add (0); lh.add (67); lh.add (66); System.out.println (lh); System.out.println ("The size of the element: "+lh.size ()); lh.clear (); System.out.println ("Set contain: " + lh.size () +" elements"); } }Output:
[99, 7, 0, 67, 66]The size of the element: 5 Set contain: 0 elements
- retainAll (): It is used to retain the entire element from the collection and show only those elements that are same in both Sets.
import java.util. LinkedHashSet; public class RetainAllExample { public static void main (String args []) { LinkedHashSet<Integer> lh= new LinkedHashSet<Integer> (); lh.add (99); lh.add (7); lh.add (0); lh.add (67); lh.add (66); System.out.println ("The element before retainAll operation: "+ lh); LinkedHashSet<Integer> lh2= new LinkedHashSet<Integer> (); lh2.add (100); lh2.add (78); lh2.add (67); lh2.add (17); lh2.add (66); System.out.println ("The element for retain: "+lh2); lh.retainAll (lh2); System.out.println ("The elements after retainAll operation: "+lh); } }Output: The element before retainAll operation:
[99, 7, 0, 67, 66]The element for retain:
[100, 78, 67, 17, 66]The elements after retainAll operation:
[67, 66]
- hashCode (): It is used to get the hashcode value of the instance of the LinkedHashSet.
import java.util. LinkedHashSet; public class HashCodeExample { public static void main (String args []) { LinkedHashSet<Integer> lh= new LinkedHashSet<Integer> (); lh.add (99); lh.add (7); lh.add (0); lh.add (67); lh.add (66); System.out.println ("The element: "+ lh); System.out.println ("The hashcode value: "+lh.hashCode ()); } }Output: The element:
[99, 7, 0, 67, 66]The hashcode value:
239
Difference between LinkedHashSet and LinkedHashMap:
LinkedHashSet |
LinkedHashMap |
1. It is used to store the collection of elements. | 1. It is used to store the key-value pairs. |
2. It implements HashSet. | 2. It Implements HashMap. |
3. It doesn’t store duplicates values. | 3. It stores unique keys but can store duplicate values. |
4. The syntax and to add the elements in the Set is: LinkedHashSet<String> lhs= new LinkedHashSet<String> (); lhs.add(“Welcome”); lhs.add(“to”); lhs.add(“TutorialAndExample”); | 4.The syntax and the add the elements in the map is: LinkedHashMap<String> lhm= new LinkedHashMap<String> (); lhm.put (“Welcome”,5); lhm.put (“to”,6); lhm.put(“TutorialAndExample”,12); |
Example using iterator() method
import java.util.*; public class SizeExample { public static void main (String args []) { LinkedHashSet<Integer> lh= new LinkedHashSet<Integer> (); lh.add (99); lh.add (7); lh.add (0); lh.add (67); lh.add (66); System.out.println ("The element: "+ lh); Iterator<Integer> i=lh.iterator (); while (i.hasNext ()) { System.out.println (i.next ()); } } }Output: The element:
[99, 7, 0, 67, 66] 99 7 0 67 66