Section 3
Learning Objectives
By the end of this lesson, learners will be able to:
Explain the Set<E> interface.
Describe the Hashset and TreeSet.
Demonstrate how to use Set Interface, TreeSet Class, and HashSet Class.
Distinguish the difference between a TreeSet and a HashSet.
Section 3
Introduction to Set<E> interface
Table of Contents
Topic 1: Overview of Set<E> Interface.
Topic 2: Classes that Implement Set<E> Interface.
Topic 3: List interface vs. Set interface.
Topic 4: Overview of HashSet<T> Class.
Topic 4a: Declare and Initialize a HashSet<E>.
Topic 4b: Common Methods of HashSet<E> Class.
Example: HashSet<E>.
Topic 5: Overview of TreeSet<E> Class.
Topic 5a: Declare and initialize a TreeSet<E>.
Topic 5b: Common Methods of TreeSet<E> Class.
Example - TreeSet<E>.
Hands-On Lab - LinkedList.
Topic 1: Overview of Set<E> Interface
Set is a Collection with NO duplicate elements, or every element of a set must be unique.
Java Set is a generic interface.
Java Set is NOT an ordered collection, which means that its elements do NOT have a particular order.
Unlike List and arrays, Set does NOT support the indexes or positions of its elements.
Set Interface allows us to perform basic mathematical set operations such as union, intersection, and subset.
Union - to get the union of the two sets x and y, we can use x.addAll(y).
Intersection - to get the intersection of the two sets x and y, we can use x.retainAll(y).
Subset - to check if x is a subset of y, we can use y.containsAll(x).
Topic 2: Classes That Implement Set<E> Interface
Set is an interface. We cannot provide the direct implementation of it because we cannot create objects from it. In order to use the functionalities of the Set<E> Interface, the Java Collections Framework (JCF) provides the following class for implementations of Set<E> interface.
EnumSet<E>: specialized for Enum types.
HashSet<E>: does not maintain any order.
TreeSet<E>: Maintains order.
The Set<E> interface is also extended by these Subinterfaces:
SortedSet.
NavigableSet.
Set extends the Collection interface.
image source: mason.gmu.edu
Topic 3: List Interface vs. Set Interface
List Interface and Set Interface differences:
List Interface allows duplicate elements, while Set Interface does not.
List Interface is ordered, while Set Interface is unordered.
a. List elements are arranged in the order of insertion.
b. Set Interface does not maintain any order.
List Interface allows element insertion, removal, and retrieval using its index, while Set Interface elements cannot be added, removed, or retrieved using index.
List Interface provides a Java iterator and ListIterator to loop through list elements. Set Interface only provides Java iterator; there are no additional iterators.
Topic 4: Overview of HashSet<T> Class
The HashSet Class of the Java Collections framework provides the functionalities of the hash table data structure.
The Hashset Class implements the Set interface, backed by a hash table.
The HashSet Class does not maintain any order, the elements would be returned in any random order.
The HashSet Class does not allow duplicates. If you try to add a duplicate element in HashSet, the old value will be overwritten.
The HashSet Class does allow null values; however, if you insert more than one null, it would still return only one null value.
Topic 4a: Declare and Initialize a HashSet<E>
When you actually declare and initialize a HashSet<E>, you should specify data type for type parameter:
HashSet<String> hsetObj = new HashSet<String>();
HashSet<Integer> hsetObj2 = new HashSet<Integer>();
HashSet<Float> hsetObj3 = new HashSet<Float>();
HashSet<UserDefineObject> hsetObj4 = new HashSet<UserDefineObject>(); In order to create a HashSet, we must import the java.util.HashSet package on top of the class.
Topic 4b: Common Methods of HashSet<E> Class
The most commonly used HashSet Methods include:
add() - Inserts the specified element to the set. Returns true if the element was added to the set, and false if otherwise.
addAll() - Inserts all the elements of the specified collection to the set.
clone() - Creates a copy of the HashSet.
contains() - Searches the HashSet for the specified element and returns a boolean result.
isEmpty() - Checks if the HashSet is empty.
size() - Returns the size of the HashSet.
clear() - Removes all of the elements from the HashSet.
Example: HashSet<E>
In this example, we will demonstrate how to create a HashSet, add elements to Hashset using the add() method, and add duplicate values to Hashset, and then iterate through iterator.
Topic 5: Overview of TreeSet<E> Class
The TreeSet<E> Class provides the functionality of a tree data structure.
The TreeSet Class implements the Set interface.
TreeSet is similar to HashSet except that TreeSet sorts the elements in the ascending order, does not maintain any order.
TreeSet does not allow duplicate values/elements.
TreeSet does not allow null objects.
TreeSet is normally used for storing huge amounts of information and data that is naturally sorted. This aids in easy and fast access.
Topic 5a: Declare and initialize a TreeSet<E>
When you actually declare and initialize a TreeSet<E>, you should specify data type for type parameter:
TreeSet<String> hsetObj = new TreeSet<String>();
TreeSet<Integer> hsetObj2 = new TreeSet<Integer>();
TreeSet<Float> hsetObj3 = new TreeSet<Float>();
TreeSet<UserDefineObject> hsetObj4 = new TreeSet<UserDefineObject>(); In order to create a tree set, we must import the java.util.TreeSet package on top of the class.
Topic 5b: Common Methods of TreeSet<E> Class
Most commonly used TreeSet Methods:
boolean add(E e): adds the specified element to the Set if not already present.
addAll(): inserts all the elements of the specified collection to the Set.
Comparator comparator(): returns the comparator used to order the elements in the Set, or null if the Set uses the natural ordering of its elements.
Object first(): returns the first (lowest) element currently in this TreeSet.
Object last(): returns the last (greatest) element currently in this TreeSet.
void clear(): removes all elements from the TreeSet.
boolean contains(Object o): returns true if the TreeSet contains the specified element; otherwise, returns false.
boolean isEmpty(): returns true if TreeSet contains no element; otherwise, returns false.
int size(): returns the number of elements in the TreeSet.
boolean remove(Object o): removes the specified element from the TreeSet if it is present and returns true; otherwise, returns false.
Object clone(): returns a shallow copy of the TreeSet.
Example - TreeSet<E>
In this example, we will demonstrate how to create a TreeSet, add elements to TreeSet using the add() method and addAll() method, and add duplicate values to Treeset.
Hands-On Lab - HashSet and TreeSet
You can find the lab GLAB - 303.11.5 - HashSet and TreeSet Processing on Canvas under the Assignment section. If you have any technical questions while performing the lab activity, you can ask the instructors for assistance.
TreeSet vs. HashSet
Both the TreeSet and the HashSet, implement the Set interface. However, there are some differences between them:
Unlike HashSet, elements in TreeSet are stored in an order. This is because TreeSet implements the SortedSet interface.
TreeSet provides methods for easy navigation, including first(), last(), headSet(), tailSet(), etc. This is because TreeSet also implements the NavigableSet interface.
HashSet is faster than the TreeSet for basic operations such as add, remove, contains, and size.
Knowledge Check
What is a Set?
What are Core Classes Implementing Set Interface?
Can we add a Null to a TreeSet or HashSet?
Last updated