HashSet and TreeSet Processing

Objective

In this lab, we will explore and demonstrate HashSet and TreeSet. We will utilize the built-in methods.

Learning Objective

By the end of this lab, learners will be able to explain and use HashSet and TreeSet.

HashSet Examples

Example One: Insert Elements to HashSet using addAll().

addAll() - Inserts all of the elements of the specified collection to the set. Create a new Java project and create a new Class named “exampleOne.” Write the code below in the class.

import java.util.HashSet;
public class exampleOne {
    public static void main(String[] args) {
        HashSet<Integer> evenNumber = new HashSet<>();
        // Using add() method
        evenNumber.add(2);
        evenNumber.add(4);
        evenNumber.add(6);
        System.out.println("HashSet: " + evenNumber);

        HashSet<Integer> numbers = new HashSet<>();
        // Using addAll() method
        numbers.addAll(evenNumber);
        numbers.add(5);
        System.out.println("New HashSet: " + numbers);
    }
}

Output:

Example Two: Union of Sets.

To perform the union between two sets, we can use the addAll() method.

Create a new Class named “exampletwo,” and then write the code below in the class.

Output

Example Three: Difference of Sets.

To calculate the difference between the two sets, we can use the removeAll() method: Create a new Class named “examplethree,” and then write the code below in the class.

Output:

Example Four: Enhanced for() Loop

Create a new Class named “examplefour,” and then write the code below in the class.

Output:

Remember: HashSet DOES NOT maintain any order, so this order might be changed if you run your code a second time.

TreeSet Examples

Example one: Iterate Through TreeSet.

To access the individual elements of TreeSet, we need to iterate through the TreeSet —— traverse through the TreeSet. We do this by declaring an Iterator for the TreeSet, and then use that Iterator to access each element. For this, we use the next() method of an iterator that returns the next element in the TreeSet.

The following Java program demonstrates the use of the Iterator to iterate through TreeSet.

Output;

Example two: Remove Elements

remove() - removes the specified element from the set. removeAll() - removes all of the elements from the set.

Output:

Example three: Methods for Navigation.

Since the TreeSet class implements NavigableSet, it provides various methods to navigate over the elements of the TreeSet.

  1. first() and last() Methods first() - returns the first element of the set. last() - returns the last element of the set.

Output:

Example four: sort the given TreeSet alphabetically in reverse order.

In this example, we will implement a Comparator class to sort the given TreeSet alphabetically in reverse order. By default, the TreeSet sorts data in ascending order. We can also sort TreeSet in a customized order by defining a new comparator class. In this comparator class, we need to override the ‘compare’ method to sort the elements of the TreeSet. This comparator object is then passed to the TreeSet constructor.

Create a class named “cities_Comparator” and add the code below:

Create a class named “TreeSetExampleCom” and add the code below:

Output:

Last updated