While we didn’t look at creating a Set, we could have with several of the data structures we built – such as the binary tree if we didn’t allow duplicate data, or by searching our linked list before adding the element.
It turns out that Java doesn’t provide a single Set type, but actually multiple types of Sets.
- Sorted Set
- Navigable Set
- Hash Set
- Linked Hash Set
- Tree Set,
- and more…
Each of these behaves as a set, but does it slightly differently, so you have to know a lot about how you intend to use your data to pick the right set style.
For our example, we’re going to use a HashSet. The basics are that it will allow you to add and remove elements.
public static void main(String args[]) {
HashSet<String> set1 = new HashSet<String>();
set1.add("Hello");
set1.add("World");
set1.add("Hello");
set1.add("Set");
set1.add("Set");
set1.add("Example");
set1.add("Hello");
System.out.println(set1);
}
Printing out the set will provide a list of values like you see with a linked list. You are not guaranteed the order of the set elements, and you will only have a single element of the same value – much like a mathematical set.
Set Style Methods
However, interestingly, Java’s HashSet, doesn’t allow for you to perform a Union, Intersection, etc. You would think those methods are obvious, but clearly, they are not.
Instead, you will have to build the logic yourself.
// Finding Intersection of set1 and set2
HashSet<String> intersectionSet = new HashSet<String>(set1);
intersectionSet.retainAll(set2);
System.out.print("The Intersection of set1 and set2 is: ");
System.out.println(intersectionSet );
// Finding Difference of set1 and set2
HashSet<String> differenceSet = new HashSet<String>(set1);
differenceSet.removeAll(set2);
System.out.print("The Difference of set1 and set2 is: ");
System.out.println(differenceSet);
// Find the Union of the two sets
HashSet<String> unionSet = new HashSet<String>(set1);
unionSet.addAll(set2);
System.out.print("The Union of set1 and set2 is: ");
System.out.println(unionSet);
Notice how you are using the ability to add data by passing in a collection to the overloaded constructor.
Then for finding the intersection, you use the retainAll()
method which retains only the elements in the set that are passed into it. So if an element doesn’t exist, it is ignored, and if it is not in the second set, it is removed – creating the intersection.
For finding the difference, you remove the elements in the second collection. removeAll()
removes all of the elements passed in, not all of the elements. Note, if you want to find the inverse, you will need to create the intersectionSet with the set2 data initially, and then removeAll with the set1 data.
Finally, for finding the union, we use the addAll()
method which allows us to pass in another set, thus creating a union.
Other Methods
Additionally, you will find several common methods such as:
clear()
which removes all data from the setcontains()
which checks to see if an element is contained within the setisEmpty()
which checks to see if the set has any elements in itsize()
which tells you the size of the set.
Java Sets was originally found on Access 2 Learn