Understanding Sets in Java

Welcome to our Java Sets lesson! In Java, sets are represented by the HashSet<E> collection, which can only hold unique elements. They are particularly useful when you need to ensure that all elements in a collection are distinct.

In this lesson, you'll learn how to create and operate on sets using HashSet<E>. You'll explore the advantages of using sets and how they can optimize performance. Let's get started!

Creating Sets

In Java, you can create a set using the HashSet<E> class. Here is an example:

import java.util.HashSet;

public class Solution {
    public static void main(String[] args) {
        HashSet<Integer> mySet = new HashSet<>();
        mySet.add(1);
        mySet.add(2);
        mySet.add(3);
        mySet.add(4);
        mySet.add(5);
        mySet.add(5); // Adding duplicate

        System.out.println(mySet); // Output: [1, 2, 3, 4, 5]
    }
}

You can use the add method to add elements to the set. Note that duplicates will be omitted, as sets can only contain unique elements.

Manipulating Sets

Java provides methods to manipulate sets, such as add, contains, remove, and clear.

import java.util.HashSet;

public class Solution {
    public static void main(String[] args) {
        HashSet<Integer> mySet = new HashSet<>();
        mySet.add(1);
        mySet.add(2);
        mySet.add(3);
        mySet.add(4);
        mySet.add(5);

        // Adding an element
        mySet.add(6); // `mySet` is now [1, 2, 3, 4, 5, 6]
        System.out.println(mySet.contains(1)); // Output: true, as `mySet` includes element 1

        // Removing an element
        mySet.remove(1); // `mySet` becomes [2, 3, 4, 5, 6]
        System.out.println(mySet.contains(1)); // Output: false, as `mySet` doesn't include 1 anymore

        // Clearing the set
        mySet.clear(); // `mySet` becomes an empty set
        System.out.println(mySet.size()); // Output: 0
    }
}
  • add: Adds a specified element to the set.
  • contains: Checks if the specified element exists in the set.
  • remove: Removes a specified element from the set.
  • clear: Removes all elements from the set.
Set Operations

Java provides built-in methods for operations such as union, intersection, and difference using addAll, retainAll, and removeAll.

import java.util.HashSet;

public class Solution {
    public static void main(String[] args) {
        HashSet<Integer> set1 = new HashSet<>();
        HashSet<Integer> set2 = new HashSet<>();
        
        // Initializing set1
        set1.add(1);
        set1.add(2);
        set1.add(3);
        set1.add(4);

        // Initializing set2
        set2.add(3);
        set2.add(4);
        set2.add(5);
        set2.add(6);

        // Set union
        HashSet<Integer> union = new HashSet<>(set1);
        union.addAll(set2);
        System.out.println(union); // Output: [1, 2, 3, 4, 5, 6]

        // Set intersection
        HashSet<Integer> intersection = new HashSet<>(set1);
        intersection.retainAll(set2);
        System.out.println(intersection); // Output: [3, 4]

        // Set difference
        HashSet<Integer> difference = new HashSet<>(set1);
        difference.removeAll(set2);
        System.out.println(difference); // Output: [1, 2]
    }
}
  • addAll: Combines elements from both sets, excluding any duplicates. This results in a set containing [1, 2, 3, 4, 5, 6].
  • retainAll: Returns a set with only the elements common to both sets. For these sets, the intersection is [3, 4].
  • removeAll: Returns a set containing elements that are in the first set but not in the second set. Here, the result is [1, 2] for set1.
Performance Benefits of Sets

One of the key advantages of sets is their faster performance in membership tests, thanks to their use of hashing.

import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;

public class Solution {
    public static void main(String[] args) {
        Random random = new Random();
        HashSet<Integer> mySet = new HashSet<>();
        List<Integer> myList = new ArrayList<>();

        for (int i = 0; i < 10000000; i++) {
            mySet.add(i);
            myList.add(i);
        }

        // Warm-up
        mySet.contains(-1);
        myList.contains(-1);

        long startTime, endTime;

        // Measure HashSet performance
        startTime = System.nanoTime();
        for (int i = 0; i < 1000; i++) {
            boolean result = mySet.contains(random.nextInt(10000000));
        }
        endTime = System.nanoTime();
        System.out.println("Set: " + ((endTime - startTime) / 1_000_000) + "ms"); // Outputs: Set: ~2ms on average

        // Measure List performance
        startTime = System.nanoTime();
        for (int i = 0; i < 1000; i++) {
            boolean result = myList.contains(random.nextInt(10000000));
        }
        endTime = System.nanoTime();
        System.out.println("List: " + ((endTime - startTime) / 1_000_000) + "ms"); // Outputs: List: ~18223ms on average
    }
}
  • Membership Test with HashSet<E>: Thanks to hash tables, sets can check for membership in constant time, leading to quick lookup times. The membership checking in the set is remarkably fast.
  • Membership Test with List: Lists require a linear search to check for membership, resulting in longer lookup times as the list grows. The membership checking in the list is noticeably slower.

Hashing is the key to HashSet's efficiency. When you attempt to add a duplicate, Java checks if the hashCode() is already in the set. If so, it compares the new element with the existing one. Each element is placed into a “bucket” based on its hashCode, so finding an element can often be done in constant time. In contrast, lists require a linear search, where each item must be checked, resulting in slower performance as the list size grows.

Lesson Summary

Congratulations! You've explored creating and manipulating sets, performing set operations, and understanding the performance benefits of sets in Java.

Remember, practice is key to solidifying your understanding. Happy coding!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal