

Set is a collection that cannot contain duplicate elements. Two sets are equal if they contain the same elements.

There are three implementations of Set

HashSet: Stores elements in hash table and is the best performing implementation. Order in iteration is not maintained.

Stores elements in hash table and is the best performing implementation. Order in iteration is not maintained. TreeSet: Stores elements in red-black tree is substantially slower than HashSet. Orders its elements based on their values.

Stores elements in red-black tree is substantially slower than HashSet. Orders its elements based on their values. LinkedHashSet: Stores elements in hash table with a linked list running through it. Orders elements based on the order in which they are inserted.

Below are bulk operations that can be done on sets

s1.containsAll(s2) – returns true if s2 is subset of s1.

s1.addAll(s2) – transforms s1 into union on s1 and s2.

s1.retainAll(s2) – transforms s1 into intersection(common elements in both) of s1 and s2.

s1.removeAll(s2) – transforms s1 into set difference of s1 and s2. (The set difference of s1 minus s2 is the set containing all of the elements found in s1 but not in s2)

Pointers:

1) If you have a collection c, and you want to create another Collection containing the same elements but with all duplicates eliminated then do the following

Colleciton<Type> noDups = new HashSet<Type>(c); 1 Colleciton <Type> noDups = new HashSet <Type> ( c ) ;

2) If above has to be done but we have to preserve the order of the original collection then do the following

Collection<Type> noDups = new LinkedHashSet<Type>(c); 1 Collection <Type> noDups = new LinkedHashSet <Type> ( c ) ;

This can be done in more generic way as shown below

public static <E> Set<E> removeDups(Collection<E> c){ return new LinkedHashSet<E>(c); } 1 2 3 public static < E > Set < E > removeDups ( Collection < E > c ) { return new LinkedHashSet < E > ( c ) ; }

Example 1: Code with HashSet, TreeSet, LinkedHashSet

Remove duplicates from an array

Rmove duplicates and sort the values from an array

Remove duplicates and maintain the order from input array

package com.srinichekuri.set.examples; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Set; import java.util.TreeSet; /** * Example to get only unique words from String * <li>Order doesn't matter - using HashSet</li> * <li>Order according to Values - using TreeSet</li> * <li>Order according to input Order - LinkedHashSet</li> * * @author Srinivas Chekuri * */ public class Example1 { public static void main(String args[]) { String test = "I am I Srini I and I I am I testing"; String[] testArray = test.split(" "); //Adding contents to HashSet will remove duplicates Set<String> hashSet = new HashSet<String>(Arrays.asList(testArray)); //Adding contents to TreeSet will remove duplicates and sort the values Set<String> treeSet = new TreeSet<String>(Arrays.asList(testArray)); //Adding contents to TreeSet will remove duplicates and will retain the order of insertion. Set<String> linkedHashSet = new LinkedHashSet<String>(Arrays.asList(testArray)); System.out.println("hashSet(No Duplicates)" + hashSet); System.out.println("treeSet(No Duplicates and sorted)" + treeSet); System.out.println("linkedHashSet(No Duplicates and order of insertion maintained)" + linkedHashSet); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com . srinichekuri . set . examples ; import java . util . Arrays ; import java . util . HashSet ; import java . util . LinkedHashSet ; import java . util . Set ; import java . util . TreeSet ; /** * Example to get only unique words from String * <li>Order doesn't matter - using HashSet</li> * <li>Order according to Values - using TreeSet</li> * <li>Order according to input Order - LinkedHashSet</li> * * @author Srinivas Chekuri * */ public class Example1 { public static void main ( String args [ ] ) { String test = "I am I Srini I and I I am I testing" ; String [ ] testArray = test . split ( " " ) ; //Adding contents to HashSet will remove duplicates Set < String > hashSet = new HashSet < String > ( Arrays . asList ( testArray ) ) ; //Adding contents to TreeSet will remove duplicates and sort the values Set < String > treeSet = new TreeSet < String > ( Arrays . asList ( testArray ) ) ; //Adding contents to TreeSet will remove duplicates and will retain the order of insertion. Set < String > linkedHashSet = new LinkedHashSet < String > ( Arrays . asList ( testArray ) ) ; System . out . println ( "hashSet(No Duplicates)" + hashSet ) ; System . out . println ( "treeSet(No Duplicates and sorted)" + treeSet ) ; System . out . println ( "linkedHashSet(No Duplicates and order of insertion maintained)" + linkedHashSet ) ; } }

hashSet(No Duplciates)[Srini, am, testing, I, and] treeSet(No Duplicates and sorted)[I, Srini, am, and, testing] linkedHashSet(No Duplicates and order of insertion maintained)[I, am, Srini, and, testing] 1 2 3 hashSet ( No Duplciates ) [ Srini , am , testing , I , and ] treeSet ( No Duplicates and sorted ) [ I , Srini , am , and , testing ] linkedHashSet ( No Duplicates and order of insertion maintained ) [ I , am , Srini , and , testing ]

Example 2: Get the list of unique values and all duplicates from an array

package com.srinichekuri.set.examples; import java.util.HashSet; import java.util.Set; /** * Example to collection all the unique and duplicate Words. * * @author Srinivas Chekuri * */ public class Example2 { public static void main(String args[]) { String test = "I am I Srini I and I I am I testing"; String[] testArray = test.split(" "); Set<String> uniques = new HashSet<String>(); Set<String> dups = new HashSet<String>(); for (String s : testArray) if (!uniques.add(s)) dups.add(s); System.out.println("Uniques: " + uniques); System.out.println("Duplicates: " + dups); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package com . srinichekuri . set . examples ; import java . util . HashSet ; import java . util . Set ; /** * Example to collection all the unique and duplicate Words. * * @author Srinivas Chekuri * */ public class Example2 { public static void main ( String args [ ] ) { String test = "I am I Srini I and I I am I testing" ; String [ ] testArray = test . split ( " " ) ; Set < String > uniques = new HashSet < String > ( ) ; Set < String > dups = new HashSet < String > ( ) ; for ( String s : testArray ) if ( ! uniques . add ( s ) ) dups . add ( s ) ; System . out . println ( "Uniques: " + uniques ) ; System . out . println ( "Duplicates: " + dups ) ; } }

Uniques: [testing, Srini, and, I, am] Duplicates: [I, am] 1 2 Uniques : [ testing , Srini , and , I , am ] Duplicates : [ I , am ]

Example 3: Example to show all the bulk operations in Set (containsAll(), addAll(), retainAll(), removeAll())

package com.srinichekuri.set.examples; import java.util.HashSet; import java.util.Set; /** * Example for Bulk operations. * * @author Srinivas Chekuri * */ public class Example3 { public static void main(String args[]) { String test = "I am I Srini I and I I am I testing"; String second = "I Srini am I is"; String[] testArray = test.split(" "); String[] secondArray = second.split(" "); Set<String> firstSet = new HashSet<String>(); Set<String> duplicate = new HashSet<String>(); Set<String> secondSet = new HashSet<String>(); for (String s : testArray) if (!firstSet.add(s)) duplicate.add(s); for(String s: secondArray) secondSet.add(s); System.out.println("firstSet: " + firstSet); System.out.println("duplicate: " + duplicate); System.out.println("secondSet: " + secondSet); //contains all if(firstSet.containsAll(duplicate)){ System.out.println("all contents in duplicate are present in firstSet"); }else{ System.out.println("all contents in duplicate are not present in firstSet"); } if(firstSet.containsAll(secondSet)){ System.out.println("all contents in secondSet are present in firstSet"); }else{ System.out.println("all contents in secondSet are not present in firstSet"); } //addAll if(firstSet.addAll(secondSet)){ System.out.println("firstSet addAll with secondSet --> " + firstSet); } //retainAll if(firstSet.retainAll(duplicate)){ System.out.println("firstSet retainAll with duplicate --> " + firstSet); } //removeAll if(firstSet.removeAll(duplicate)){ System.out.println("firstSet removeAll with duplicate --> " + firstSet); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 package com . srinichekuri . set . examples ; import java . util . HashSet ; import java . util . Set ; /** * Example for Bulk operations. * * @author Srinivas Chekuri * */ public class Example3 { public static void main ( String args [ ] ) { String test = "I am I Srini I and I I am I testing" ; String second = "I Srini am I is" ; String [ ] testArray = test . split ( " " ) ; String [ ] secondArray = second . split ( " " ) ; Set <String> firstSet = new HashSet <String> ( ) ; Set <String> duplicate = new HashSet <String> ( ) ; Set <String> secondSet = new HashSet <String> ( ) ; for ( String s : testArray ) if ( ! firstSet . add ( s ) ) duplicate . add ( s ) ; for ( String s : secondArray ) secondSet . add ( s ) ; System . out . println ( "firstSet: " + firstSet ) ; System . out . println ( "duplicate: " + duplicate ) ; System . out . println ( "secondSet: " + secondSet ) ; //contains all if ( firstSet . containsAll ( duplicate ) ) { System . out . println ( "all contents in duplicate are present in firstSet" ) ; } else { System . out . println ( "all contents in duplicate are not present in firstSet" ) ; } if ( firstSet . containsAll ( secondSet ) ) { System . out . println ( "all contents in secondSet are present in firstSet" ) ; } else { System . out . println ( "all contents in secondSet are not present in firstSet" ) ; } //addAll if ( firstSet . addAll ( secondSet ) ) { System . out . println ( "firstSet addAll with secondSet --> " + firstSet ) ; } //retainAll if ( firstSet . retainAll ( duplicate ) ) { System . out . println ( "firstSet retainAll with duplicate --> " + firstSet ) ; } //removeAll if ( firstSet . removeAll ( duplicate ) ) { System . out . println ( "firstSet removeAll with duplicate --> " + firstSet ) ; } } }

firstSet: [Srini, am, testing, I, and] duplicate: [am, I] secondSet: [Srini, is, am, I] all contents in duplicate are present in firstSet all contents in secondSet are not present in firstSet firstSet addAll with secondSet --> [Srini, is, am, testing, I, and] firstSet retainAll with duplicate --> [am, I] firstSet removeAll with duplicate --> [] 1 2 3 4 5 6 7 8 firstSet : [ Srini , am , testing , I , and ] duplicate : [ am , I ] secondSet : [ Srini , is , am , I ] all contents in duplicate are present in firstSet all contents in secondSet are not present in firstSet firstSet addAll with secondSet -- > [ Srini , is , am , testing , I , and ] firstSet retainAll with duplicate -- > [ am , I ] firstSet removeAll with duplicate -- > [ ]

Hope this post was helpful. Let me know if you need any specific examples and I will work to see if I can post them.

Let’s learn and Share together.