• A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.


  • The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.


  • Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.


  • Two Set instances are equal if they contain the same elements The Java platform contains three general-purpose Set implementations: HashSet, TreeSet, and LinkedHashSet.


    1. HashSet, which stores its elements in a hash table, is the best-performing implementation; however it makes no guarantees concerning the order of iteration.


    2. TreeSet, which stores its elements in a red-black tree, orders its elements based on their values; it is substantially slower than HashSet.


    3. LinkedHashSet, which is implemented as a hash table with a linked list running through it, orders its elements based on the order in which they were inserted into the set (insertion-order).


    4. LinkedHashSet spares its clients from the unspecified, generally chaotic ordering provided by HashSet at a cost that is only slightly higher.


     
    
    import java.util.*; 
    public class Set_example 
    { 
        public static void main(String[] args) 
        { 
            
      
            
            // Set demonstration using HashSet 
            Set hash_Set = new HashSet(); 
            hash_Set.add("Pranav"); 
            hash_Set.add("Nerurkar"); 
            hash_Set.add("Is"); 
            hash_Set.add("Using"); 
            hash_Set.add("Set"); 
            hash_Set.add("And"); 
            hash_Set.add("Also");
            hash_Set.add("Using"); 
            hash_Set.add("Hash Set");
            hash_Set.add("And"); 
            hash_Set.add("Tree Set");
            
            
            System.out.println("Hash Set output without the duplicates"); 
            System.out.println(hash_Set); 
      
            // Set demonstration using TreeSet 
            System.out.println("Sorted Set after passing into TreeSet");
            
            Set tree_Set = new TreeSet(hash_Set); 
            System.out.println(tree_Set); 
            
             // Set demonstration using Linked hash Set 
            System.out.println("Set after passing into Linked Hash Set");
            
            
            Set linked_hash_Set = new LinkedHashSet(hash_Set); 
            System.out.println(linked_hash_Set); 
            
            
        } 
    } 
    
    
    
    Output:
    
    Hash Set output without the duplicates
    [Tree Set, Pranav, Set, And, Using, Hash Set, Also, Is, Nerurkar]
    Sorted Set after passing into TreeSet
    [Also, And, Hash Set, Is, Nerurkar, Pranav, Set, Tree Set, Using]
    Sorted Set after passing into Linked Hash Set
    [Tree Set, Pranav, Set, And, Using, Hash Set, Also, Is, Nerurkar]
    
    
    • The size operation returns the number of elements in the Set (its cardinality).


    • The isEmpty method does exactly what you think it would.


    • The add method adds the specified element to the Set if it is not already present and returns a boolean indicating whether the element was added.


    • Similarly, the remove method removes the specified element from the Set if it is present and returns a boolean indicating whether the element was present.


    • The iterator method returns an Iterator over the Set.


    • The following program prints out all distinct words in its argument list.


    •  
      import java.util.*;
      
      public class Sample {
          public static void main(String[] args) {
              Set hash_Set = new HashSet();
              hash_Set.add("Pranav"); 
              hash_Set.add("Nerurkar"); 
              hash_Set.add("Is"); 
              hash_Set.add("Using"); 
              hash_Set.add("Set"); 
              hash_Set.add("And"); 
              hash_Set.add("Also");
              hash_Set.add("Using"); 
              hash_Set.add("Hash Set");
              hash_Set.add("And"); 
              hash_Set.add("Tree Set");
              for (String a : hash_Set)
                     System.out.println(a);
                     
              Iterator iterator = hash_Set.iterator(); 
        
              System.out.println("List elements using iterator: "); 
        
              while (iterator.hasNext()) 
                  System.out.print(iterator.next() + " "); 
                  
              System.out.println();
              System.out.println("Set size ="+hash_Set.size());
              System.out.println("Set is empty ="+hash_Set.isEmpty());
              System.out.println("Set remove ="+hash_Set.remove("Set")); 
          }
      }
      
      Output:
      Tree Set
      Pranav
      Set
      And
      Using
      Hash Set
      Also
      Is
      Nerurkar
      List elements using iterator: 
      Tree Set Pranav Set And Using Hash Set Also Is Nerurkar 
      Set size =9
      Set is empty =false
      Set remove =true
      
      
    • Bulk operations are particularly well suited to Sets; when applied, they perform standard set-algebraic operations. Suppose s1 and s2 are sets. Here's what bulk operations do:


      1. s1.containsAll(s2) — returns true if s2 is a subset of s1. (s2 is a subset of s1 if set s1 contains all of the elements in s2.)


      2. s1.addAll(s2) — transforms s1 into the union of s1 and s2. (The union of two sets is the set containing all of the elements contained in either set.)


      3. s1.retainAll(s2) — transforms s1 into the intersection of s1 and s2. (The intersection of two sets is the set containing only the elements common to both sets.)


      4. s1.removeAll(s2) — transforms s1 into the (asymmetric) set difference of s1 and s2. (For example, the set difference of s1 minus s2 is the set containing all of the elements found in s1 but not in s2.)


       
      import java.util.*;
      
      public class FindDups {
          public static void main(String[] args) {
              Set hash_Set_A = new HashSet();
              Set hash_Set_B = new HashSet();
              hash_Set_A.add("A"); 
              hash_Set_A.add("B"); 
              hash_Set_A.add("C"); 
              hash_Set_A.add("D"); 
              hash_Set_A.add("E"); 
              
              
              hash_Set_B.add("A"); 
              hash_Set_B.add("B");
              hash_Set_B.add("C"); 
              hash_Set_B.add("D");
              hash_Set_B.add("E"); 
              hash_Set_B.add("F");
              
              
              // hash_Set_A contains A,B,C,D,E and hash_Set_B contains A,B,C,D,E,F
              System.out.println(hash_Set_A);
              System.out.println(hash_Set_B);
              
              
              System.out.println(hash_Set_A.containsAll(hash_Set_B));
              
              System.out.println(hash_Set_A.addAll(hash_Set_B));
              
              System.out.println(hash_Set_A.retainAll(hash_Set_B));
              
              System.out.println(hash_Set_A.removeAll(hash_Set_B));
              
              
              
          }
      }
      
      
      Output
      [A, B, C, D, E]
      [A, B, C, D, E, F]
      false
      true
      false
      true