Understanding Sets in Java

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.
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