Java HashSet Methods

Beginner
6 min read
Updated: Nov 2026
17 method guides
Method index

About This Index

This page is the Java HashSet Methods index. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example is a complete Java class with a main method and fixed data, and all of them compile on Java 8 and later.

New to sets? A HashSet (in java.util, since Java 1.2) is a collection of unique elements: adding something that is already there changes nothing. It keeps its elements in a hash table, so add(), contains(), and remove() take constant time on average. It allows one null element, keeps no guaranteed order, and is not thread-safe.

No guaranteed order.

A HashSet prints and iterates its elements in the order of its internal buckets, not in insertion or alphabetical order, and that order can change as the set grows. Examples that print a whole set show the typical output on OpenJDK 8–21 and say so; many examples print through a sorted TreeSet copy or a sorted list instead.

How to Call a HashSet Method

Every method here is an instance method, so you call it on your set variable:

Java
import java.util.HashSet;
import java.util.Set;

Set<String> tags = new HashSet<>();
boolean first = tags.add("java");        // true: it was added
boolean again = tags.add("java");        // false: already there
boolean has = tags.contains("java");     // true
int count = tags.size();                 // 1
boolean gone = tags.remove("java");      // true: it was removed

Java HashSet Methods Table

All method guides in this Java tutorial series (17 tutorials), grouped by purpose.

Add and look up

4 methods

Put elements into the set, add a whole collection at once, and test whether one or many elements are already there. Lookups are fast.

MethodDescriptionTutorial
add()Add one element; returns false for a duplicate.Try it
addAll()Add every element of another collection.Try it
contains()Check whether an element is in the set.Try it
containsAll()Check that every element of another collection is present.Try it

Remove and filter

5 methods

Delete one element, delete every element found in another collection, delete by a condition, keep only a shared part, or empty the set.

MethodDescriptionTutorial
remove()Delete one element; returns whether it was there.Try it
removeAll()Remove every element found in another collection.Try it
removeIf()Remove every element that passes a test (Java 8).Try it
retainAll()Keep only the elements found in another collection.Try it
clear()Remove every element from the set.Try it

Loop and convert

3 methods

Walk through the elements one by one, hand them to a Java 8 spliterator, or copy them into an array. All of them follow the same unguaranteed hash order.

MethodDescriptionTutorial
iterator()Walk through the elements one at a time.Try it
spliterator()Get a Java 8 Spliterator that can step or split.Try it
toArray()Copy the elements into an array.Try it

Copy and compare

3 methods

Make a shallow copy of the set, compare two sets for equal contents, or get the hash code of the whole set.

MethodDescriptionTutorial
clone()Make a shallow copy of the set.Try it
equals()Compare two sets by their contents.Try it
hashCode()Get one int that sums up the whole set.Try it

Size

2 methods

Count the elements or check whether there are none.

MethodDescriptionTutorial
size()Count the elements in the set.Try it
isEmpty()Check whether the set has no elements.Try it

Frequently Asked Questions

They are the methods of java.util.HashSet, a collection of unique elements kept in a hash table. They add elements, check whether an element is present, remove elements, loop over them, convert them to an array, and compare or copy a whole set.
This index lists 17 guides, from add() and contains() to retainAll(), removeIf(), and spliterator(), grouped by what they do.
removeIf() and spliterator(). removeIf() is a default method that HashSet inherits from Collection, and spliterator() is overridden by HashSet. Every other method here works on Java 1.2 and later.
A HashSet stores elements in buckets chosen from their hash codes and prints them bucket by bucket. The order is not guaranteed and can change as the set grows. Use LinkedHashSet for insertion order or TreeSet for sorted elements.
Open add(), contains(), and remove() first. Then try retainAll() and removeIf() from the tables below.

Start with add()

Add your first elements and see what add() returns for a duplicate.

add() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful