← Back to Core Java Course | Chapter 11: Collections | Lesson 8 of 17

Java Set Interface

The Set interface represents a collection that contains no duplicate elements, used whenever the uniqueness of the stored values matters more than the order they were added in.
Syntax
java
Set<Type> set = new HashSet<>();
set.add(item);
set.contains(item);

What is the Set Interface?

The Set interface represents a collection that contains no duplicate elements, and is used whenever the uniqueness of the stored values matters more than the order they were added in.

Example: What is the Set Interface?

java
import java.util.Set;
import java.util.HashSet;
public class Main {
	public static void main(String[] args) {
		Set<String> names = new HashSet<>();
		names.add("Alice");
		names.add("Alice"); // duplicate ignored
		System.out.println(names.size());
	}
}

Set Implementations

HashSet, LinkedHashSet, and TreeSet are the three standard implementations of Set: HashSet offers the fastest operations with no ordering guarantee, LinkedHashSet preserves insertion order, and TreeSet keeps elements sorted.

Example: Set Implementations

java
import java.util.Set;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;
public class Main {
	public static void main(String[] args) {
		Set<String> hash = new HashSet<>();
		Set<String> linked = new LinkedHashSet<>(); // insertion order
		Set<String> tree = new TreeSet<>(); // sorted
		hash.add("a"); linked.add("a"); tree.add("a");
		System.out.println(hash + " " + linked + " " + tree);
	}
}

No Duplicates Allowed

Every implementation of Set silently ignores an attempt to add a value that's already present -- the add method returns false instead of throwing an error, and the set's size never grows from a duplicate insertion.

Example: No Duplicates Allowed

java
import java.util.Set;
import java.util.HashSet;
public class Main {
	public static void main(String[] args) {
		Set<String> names = new HashSet<>();
		System.out.println(names.add("Alice")); // true
		System.out.println(names.add("Alice")); // false: already present
	}
}

Common Set Methods

The Set interface defines methods like add, remove, contains, and size that work the same way across every implementation, letting code switch between HashSet, TreeSet, or LinkedHashSet without changing how it interacts with the set.

Example: Common Set Methods

java
// Import java.util.Set so it can be used by its short name
import java.util.Set;
// Import java.util.HashSet so it can be used by its short name
import java.util.HashSet;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new HashSet object and store it in names
		Set<String> names = new HashSet<>();
		names.add("Alice");
		// Print a line to the console
		System.out.println(names.contains("Alice"));
		// Print a line to the console
		System.out.println(names.size());
		names.remove("Alice");
		System.out.println(names.contains("Alice"));
	}
}

Choosing a Set Implementation

Choosing between Set implementations comes down to what guarantee you need: HashSet for the fastest general-purpose set, LinkedHashSet when insertion order must be remembered, and TreeSet when the elements must always stay sorted.

Example: Choosing a Set Implementation

java
import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
public class Main {
	public static void main(String[] args) {
		Set<Integer> fast = new HashSet<>(); // fastest, no order
		Set<Integer> sorted = new TreeSet<>(); // sorted order
		fast.add(3);
		sorted.add(3);
		System.out.println(fast + " " + sorted);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting a set to keep insertion order, which HashSet does not.
  2. Trying to get an element by index, which sets do not support.
  3. Adding objects without proper equals() and hashCode(), so duplicates are not detected.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.