Java Program to Implement LinkedHashSet API

This Java program is to Implement LinkedHashSet Collection API. Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set.

Here is the source code of the Java program to Implement LinkedHashSet Collection API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.Collection;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4. import java.util.LinkedHashSet;
  5. import java.util.Set;
  6.  
  7. public class LinkedHashSetImp<E>
  8. {
  9.     private LinkedHashSet<E> linkedHashSet;
  10.  
  11.     /*
  12.      * Constructs a new, empty linked hash set with the default initial capacity
  13.      * (16) and load factor (0.75).
  14.      */
  15.     public LinkedHashSetImp()
  16.     {
  17.         linkedHashSet = new LinkedHashSet<E>();
  18.     }
  19.  
  20.     /*
  21.      * Constructs a new linked hash set with the same elements as the specified
  22.      * collection.
  23.      */
  24.     public LinkedHashSetImp(Collection<? extends E> c)
  25.     {
  26.         linkedHashSet = new LinkedHashSet<E>(c);
  27.     }
  28.  
  29.     /*
  30.      * Constructs a new, empty linked hash set with the specified initial
  31.      * capacity and the default load factor (0.75).
  32.      */
  33.     public LinkedHashSetImp(int initialCapacity)
  34.     {
  35.         linkedHashSet = new LinkedHashSet<E>(initialCapacity);
  36.     }
  37.  
  38.     /*
  39.      * Constructs a new, empty linked hash set with the specified initial
  40.      * capacity and load factor.
  41.      */
  42.     public LinkedHashSetImp(int initialCapacity, float loadFactor)
  43.     {
  44.         linkedHashSet = new LinkedHashSet<E>(initialCapacity, loadFactor);
  45.     }
  46.  
  47.     /* Returns the number of elements in this set (its cardinality). */
  48.     public int size()
  49.     {
  50.         return linkedHashSet.size();
  51.     }
  52.  
  53.     /* Returns true if this set contains no elements. */
  54.     public boolean isEmpty()
  55.     {
  56.         return linkedHashSet.isEmpty();
  57.     }
  58.  
  59.     /*
  60.      * Returns true if this set contains the specified element i.e
  61.      * returns true if and only if this set contains an element e such that
  62.      * (o==null ? e==null : o.equals(e)).
  63.      */
  64.     public boolean contains(Object o)  
  65.       throws ClassCastException,NullPointerException
  66.     {
  67.         return linkedHashSet.contains(o);
  68.     }
  69.  
  70.     /* Returns an iterator over the elements in this set */
  71.     public Iterator<E> iterator()
  72.     {
  73.         return linkedHashSet.iterator();
  74.     }
  75.  
  76.     /* Returns an array containing all of the elements in this set. */
  77.     public Object[] toArray()
  78.     {
  79.         return linkedHashSet.toArray();
  80.     }
  81.  
  82.     /*
  83.      * Returns an array containing all of the elements in this set; the runtime
  84.      * type of the returned array is that of the specified array
  85.      */
  86.     public <T> T[] toArray(T[] a)
  87.       throws ArrayStoreException,NullPointerException
  88.     {
  89.         return linkedHashSet.toArray(a);
  90.     }
  91.  
  92.     /* Adds the specified element to this set if it is not already present */
  93.     public boolean add(E e)
  94.       throws UnsupportedOperationException,ClassCastException, NullPointerException, IllegalArgumentException     
  95.     {
  96.         return linkedHashSet.add(e);
  97.     }
  98.  
  99.     /* Removes the specified element from this set if it is present */
  100.     public boolean remove(Object o)
  101.       throws ClassCastException,NullPointerException, UnsupportedOperationException
  102.     {
  103.         return linkedHashSet.remove(o);
  104.     }
  105.  
  106.     /*
  107.      * Returns true if this set contains all of the elements of the specified
  108.      * collection
  109.      */
  110.     public boolean containsAll(Collection<?> c)
  111.       throws ClassCastException,NullPointerException
  112.     {
  113.         return linkedHashSet.containsAll(c);
  114.     }
  115.  
  116.     /*
  117.      * Adds all of the elements in the specified collection to this set if
  118.      * they're not already present
  119.      */
  120.     public boolean addAll(Collection<? extends E> c)
  121.       throws UnsupportedOperationException, ClassCastException,NullPointerException, IllegalArgumentException    
  122.     {
  123.         return linkedHashSet.addAll(c);
  124.     }
  125.  
  126.     /*
  127.      * Retains only the elements in this set that are contained in the specified
  128.      * collection
  129.      */
  130.     public boolean retainAll(Collection<?> c)
  131.       throws UnsupportedOperationException, ClassCastException,NullPointerException
  132.     {
  133.         return linkedHashSet.retainAll(c);
  134.     }
  135.  
  136.     /*
  137.      * Removes from this set all of its elements that are contained in the
  138.      * specified collection
  139.      */
  140.     public boolean removeAll(Collection<?> c)
  141.       throws UnsupportedOperationException, NullPointerException,ClassCastException
  142.     {
  143.         return linkedHashSet.retainAll(c);
  144.     }
  145.  
  146.     /* Removes all of the elements from this set */
  147.     public void clear()
  148.       throws UnsupportedOperationException
  149.     {
  150.         linkedHashSet.clear();
  151.     }
  152.  
  153.     /* Compares the specified object with this set for equality */
  154.     public boolean equals(Object o)
  155.     {
  156.         return linkedHashSet.equals(o);
  157.     }
  158.  
  159.     /* Returns the hash code value for this set */
  160.     public int hashCode()
  161.     {
  162.         return linkedHashSet.hashCode();
  163.     }
  164.  
  165.     public static void main(String... arg)
  166.     {
  167.         LinkedHashSetImp<Integer> linkedHashSet = new LinkedHashSetImp<Integer>();
  168.         linkedHashSet.add(10);
  169.         linkedHashSet.add(20);
  170.         linkedHashSet.add(30);
  171.         linkedHashSet.add(40);
  172.         linkedHashSet.add(50);
  173.  
  174.         System.out.println("the size of the linkedhashset is : " + linkedHashSet.size());
  175.  
  176.         System.out.println("the elements in linkedhashset are :");
  177.         Iterator<Integer> iterator = linkedHashSet.iterator();
  178.         while(iterator.hasNext())
  179.         { 
  180.             System.out.print(iterator.next() + "\t");
  181.         }
  182.         System.out.println();
  183.  
  184.         System.out.println("element 40 removed is " + linkedHashSet.remove(40));
  185.         if(linkedHashSet.isEmpty())
  186.             System.out.println("the linkedHashset is empty");
  187.         else
  188.             System.out.println("the linkedhashset is not empty");
  189.  
  190.         Set<Integer> retainedSet = new HashSet<Integer>();
  191.         retainedSet.add(10);
  192.         retainedSet.add(20);
  193.  
  194.         System.out.println("the elements retained in LinkedhashSet");
  195.         linkedHashSet.retainAll(retainedSet);
  196.         Iterator<Integer> riterator = linkedHashSet.iterator();
  197.         while(riterator.hasNext())
  198.         {
  199.             System.out.print(riterator.next() + "\t");
  200.         }
  201.         System.out.println();
  202.  
  203.         linkedHashSet.clear();
  204.         System.out.println("the linkedHashSet cleared");
  205.         if(linkedHashSet.isEmpty())
  206.             System.out.println("the linkedHashSet is empty");
  207.         else
  208.             System.out.println("the linkedHashSet is not empty");
  209.  
  210.     }
  211. }


$javac LinkedHashSetImp.java
$java LinkedHashSetImp
 
the size of the linkedhashset is : 5
the elements in linkedhashset are :
 10	20	30	40	50	
element 40 removed is true
the linkedhashset is not empty
the elements retained in LinkedhashSet
10	20	
the linkedHashSet cleared
the linkedHashSet is empty

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.