Another core interface provided by the collection framework is the Set
interface. A Set
is a collection of unique elements and all of its methods ensure this stays true. The collection framework provides different implementations of a Set
(we’ll focus on a subset of them) that each have different use cases.
The HashSet
implementation has the best performance when retrieving or inserting elements but cannot guarantee any ordering among them.
The TreeSet
implementation does not perform as well on insertion and deletion of elements but does keep the elements stored in order based on their values (this can be customized).
The LinkedHashSet
implementation has a slightly slower performance on insertion and deletion of elements than a HashSet
but keeps elements in insertion order.
Let’s look at how we can create Set
with a HashSet
implementation:
Set<Integer> intSet = new HashSet<>(); // Empty set intSet.add(6); // true - 6 intSet.add(0); // true - 0, 6 (no guaranteed ordering) intSet.add(6); // false - 0, 6 (no change, no guaranteed ordering) boolean isNineInSet = intSet.contiains(9); // false boolean isSixInSet = intSet.contains(0); // true
In the example above we:
- Created a
Set
reference namedintSet
with aHashSet
implementation. - Called
add()
, which adds elements to theSet
and returnstrue
if an element was successfully added orfalse
if not. - Called
add()
, noting that the program will not throw an error if we try to insert a non-unique element into the set. - Called
contains(9)
, which returnsfalse
because the9
does not exist inintSet
. - Called
contains(9)
, which returnstrue
because the0
does exist inintSet
.
We can iterate through a Set
using the enhanced for-loop
and notice that we can’t guarantee the ordering of elements looped. For example:
// Assuming `intSet` has elements -> 1, 5, 9, 0, 23 for (Integer number: intSet) { System.out.println(number); } // OUTPUT TERMINAL: 5 0 23 9 1
Let’s practice creating a Set
and iterating through it.
Instructions
Let’s create a collection of unique words that are stored in insertion order.
In main()
of Main.java, create String
type Set
with an implementation of Set
that stores elements in insertion order named stringSet
.
Let’s add elements in our collection.
In main()
, call add()
on stringSet
and add the following elements: "Hello"
, "World"
, "Hello"
.
Let’s print out all the elements in our Set
.
After adding String
s to stringSet
, iterate through stringSet
with an enhanced for-loop
using a String
reference named element
. In the body of the for-loop
call System.out.println()
and output element
to the terminal.