๐ŸŽ“ A1Learner
๐Ÿ”
0 of 40 lessons completed

Java HashSet in Depth

A HashSet stores each value at most once, with no positions. Bulk operations combine two sets, and each result below prints through a sorted copy.

Union with addAll

addAll copies every value of the second set into the first. The sorted copy prints each shared value once.

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
  public static void main(String[] args) {
    Set<Integer> first = new HashSet<>();
    first.add(1);
    first.add(2);
    first.add(3);
    Set<Integer> second = new HashSet<>();
    second.add(3);
    second.add(4);
    second.add(5);
    first.addAll(second);
    List<Integer> sorted = new ArrayList<>(first);
    Collections.sort(sorted);
    System.out.println(sorted);
  }
}

Intersection with retainAll

retainAll keeps only the values the second set also holds. One value survives the crossing of the two sets.

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
  public static void main(String[] args) {
    Set<Integer> first = new HashSet<>();
    first.add(1);
    first.add(2);
    first.add(3);
    Set<Integer> second = new HashSet<>();
    second.add(3);
    second.add(4);
    second.add(5);
    Set<Integer> shared = new HashSet<>(first);
    shared.retainAll(second);
    List<Integer> sorted = new ArrayList<>(shared);
    Collections.sort(sorted);
    System.out.println(sorted);
  }
}

Difference with removeAll

removeAll deletes every value that the second set holds. The values 1 and 2 remain in the first set.

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
  public static void main(String[] args) {
    Set<Integer> first = new HashSet<>();
    first.add(1);
    first.add(2);
    first.add(3);
    Set<Integer> second = new HashSet<>();
    second.add(3);
    second.add(4);
    second.add(5);
    Set<Integer> rest = new HashSet<>(first);
    rest.removeAll(second);
    List<Integer> sorted = new ArrayList<>(rest);
    Collections.sort(sorted);
    System.out.println(sorted);
  }
}

A TreeSet stays sorted

A TreeSet sorts each value as it arrives. Adding 5 a second time changes nothing, so the size stays three.

import java.util.Set;
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    Set<Integer> sorted = new TreeSet<>();
    sorted.add(5);
    sorted.add(2);
    sorted.add(3);
    sorted.add(5);
    System.out.println(sorted);
    System.out.println(sorted.size());
  }
}

Example

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
  public static void main(String[] args) {
    Set<String> morning = new HashSet<>();
    morning.add("Ana");
    morning.add("Ben");
    morning.add("Cid");
    Set<String> afternoon = new HashSet<>();
    afternoon.add("Ben");
    afternoon.add("Cid");
    afternoon.add("Dan");
    Set<String> everyone = new HashSet<>(morning);
    everyone.addAll(afternoon);
    List<String> allSorted = new ArrayList<>(everyone);
    Collections.sort(allSorted);
    System.out.println(allSorted);
    Set<String> shared = new HashSet<>(morning);
    shared.retainAll(afternoon);
    List<String> sharedSorted = new ArrayList<>(shared);
    Collections.sort(sharedSorted);
    System.out.println(sharedSorted);
    Set<String> morningOnly = new HashSet<>(morning);
    morningOnly.removeAll(afternoon);
    List<String> onlySorted = new ArrayList<>(morningOnly);
    Collections.sort(onlySorted);
    System.out.println(onlySorted);
  }
}

Key points

  • addAll forms the union, every value that appears in either set.
  • retainAll forms the intersection, only the values present in both sets.
  • removeAll subtracts one set from another, deleting every shared value.
  • A HashSet keeps no order, so every listing here sorts a copy before printing.
  • A TreeSet is a set that keeps its values sorted as they are added.

In this example, addAll builds the full roster, retainAll finds the people in both groups, and removeAll leaves only the morning group.

Note

Adding a value that a set already holds changes nothing, in a HashSet or a TreeSet.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which operation keeps only the values present in both sets?