A set is a member of the Java collections framework that only
stores unique elements. So members
of a Set implement the Set interface. Here’s how to declare a set:
HashSet
Set<String> nameSet
= new HashSet<String>();
We can add several elements to the set using the add() method
like this:
nameSet.add(“Veronicah”);
nameSet.add(“Brian”);
nameSet.add(“Tyrese”);
nameSet.add(“Gibson”);
nameSet.add(“Anthony”);
Figure 1: Adding items to a set
Remember we said that sets only store unique elements. So
what happens when we try to add duplicate items like this:
Figure 2: Trying to add a duplicate item to a set
Well, it does nothing. Java will only output the unique
elements. This means that instead of outputting two elements “Brian”, it will
only output one “Brian.”
HashSet doesn’t remember any kind of order. Therefore,
HashSets will rearrange themselves randomly from time to time. HashSets are,
therefore, not ideal when you want some order in the elements added to it.
LinkedHashSet
Use a LinkedHashSet if you want items put in a particular
order. With a LinkedHashSet, you have unique elements placed in a specific
order. A LinkedHashSet is just like a HashSet, only that it has a doubly-linked
list between all the elements.
Set<String> nameSet
= new LinkedHashSet<String>();
We can add several elements to the set using the add() method
like this:
nameSet.add(“Veronicah”);
nameSet.add(“Brian”);
nameSet.add(“Tyrese”);
nameSet.add(“Gibson”);
nameSet.add(“Anthony”);
Now, if we want to output the contents of the set, we can
just use System.out.println(nameSet); because sets have a toString() method.
Your output will be as shown in the figure below.
Remember we said that sets only store unique elements. So
what happens when we try to add duplicate items like this:
nameSet.add(“Brian”);
Figure 3: Adding items to a LinkedHashSet, adding duplicate items to a LinkedHashSet
TreeSet
If you want your set to be ordered in natural order, use a
TreeSet. What a TreeSet does, is that it allows you to store unique elements,
but in a natural order.
Set<String> nameSet
= new TreeSet<String>();
We can add several elements to the set using the add() method
like this:
nameSet.add(“Veronicah”);
nameSet.add(“Brian”);
nameSet.add(“Tyrese”);
nameSet.add(“Gibson”);
nameSet.add(“Anthony”);
Now, if we want to output the contents of the set, we can
just use System.out.println(nameSet); because sets have a toString() method.
Your output will be as shown in the figure below.
Remember we said that sets only store unique elements. So
what happens when we try to add duplicate items like this:
nameSet.add(“Brian”);
Well, it does nothing. Java will only output the unique
elements. This means that instead of outputting two elements “Brian”, it will
only output one “Brian.”
As
you can see in the figure below, the TreeSet outputs unique items, but in an
ordered manner, which in this case is an ascending numerical order. Figure 4: A TreeSet displays unique items in their natural order
Iterating through Sets
You can iterate through any set using a foreach loop like
this:
for(String name: nameSet) {
System.out.println(name);
}Figure 5: Iterating through sets using a foreach loop
Finding out
whether a Set has a Particular Item
Sets are optimized for quickly finding whether an item
exists. We use the contains() method where we place the element we are
searching for in the parentheses. So let’s say we are looking to see whether
the name “Brian” is actually in the set:
if(nameSet.contains(“Brian”) ){
System.out.println
(“nameSet contains “Brian”);
}Figure 6: Finding out if an item exists in a set
If the item is indeed there, then the above sentence will be
output in the console. Otherwise, nothing will be printed.
Finding out
if a Set is Empty
If you want to find out if a set is empty, you can use the
isEmpty() method.
if (nameSet.isEmpty()) {
System.out.println
(“nameSet is empty.”);
}
Figure 7: Finding out if a set is empty
Intersection
of Sets
An intersection refers to the element or elements in two sets
that are equal. In other words, it is where the two sets meet. So to
demonstrate this, we are going to create another set.
Set<String> nameSet1
= new TreeSet<String>();
We can add several elements to the set using the add() method
like this:
nameSet1.add(“Veronicah”);
nameSet1.add(“Dan”);
nameSet1.add(“Bob”);
nameSet1.add(“Mark”);
nameSet1.add(“Bean”);
The only element that is common in both nameSet and nameSet1
is “Veronicah”.
It is advisable that before checking whether two sets have an
intersection or not, you create a copy of one of the sets to avoid messing up
your two sets. So what we can do is create a new HashSet and in its
constructor, pass in the first set, i.e. nameSet1 like this:
Set<String> intersection = new HashSet<String>(nameSet);
So what we do in the above line is create a new HashSet and
make intersection a copy of nameSet.
Now, we want to find out which elements are in the set labeled
“intersection” that are also in nameSet1. We use the retainAll() method. So we can say:
intersection.retainAll(nameSet1);
So we can surround the above statement with a
System.out.println() to output the result to the console.
System.out.println(intersection.retainAll(nameSet1));
Figure 8: Finding out if two sets have common items, i.e. intersection
Finding the
Difference between Two Sets
A difference refers to the element or elements in one set
that are not in the other set. In other words, it is where the two sets meet.
So to demonstrate this, we are going to create another set.
Set<String> nameSet1
= new TreeSet<String>();
We can add several elements to the set using the add() method
like this:
nameSet1.add(“Veronicah”);
nameSet1.add(“Dan”);
nameSet1.add(“Bob”);
nameSet1.add(“Mark”);
nameSet1.add(“Bean”);
The only element that is common in both nameSet and nameSet1
is “Veronicah”.
It is advisable that before checking whether two sets have an
difference or not, you create a copy of one of the sets to avoid messing up
your two sets. So what we can do is create a new HashSet and in its
constructor, pass in the first set, i.e. nameSet1 like this:
Set<String> difference = new
HashSet<String>(nameSet);
So what we do in the above line is create a new HashSet and
make difference a copy of nameSet.
Now, we want to find out which elements are in the set
labeled “difference” that are also in nameSet1. We use the removeAll() method. So we can say:
difference.removeAll(nameSet1);
So we can surround the above statement with a System.out.println()
to output the result to the console.
System.out.println(difference.removeAll(nameSet1));
Figure 9: Finding the difference between two sets
In the next tutorial, we are going to look at how to use your
own objects in sets and as keys in HashMap, which will require us to implement
the hashCode() and equals() methods.
If you have any questions or comments, please post them in
the comments section below and I will address them.
Until next time, take care.
No comments:
Post a Comment