Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. new HashSet<>() / set.add() / contains() / remove()

new HashSet<>() / set.add() / contains() / remove()

A set is a collection that does not allow duplicate elements. HashSet is the most common set implementation, and does not guarantee element order. Use LinkedHashSet to maintain insertion order, or TreeSet to keep elements in natural (sorted) order.

Syntax

// Creates a HashSet.
Set<Type> setName = new HashSet<>();

// Creates a set that maintains insertion order.
Set<Type> setName = new LinkedHashSet<>();

// Creates a set that keeps elements in ascending order.
Set<Type> setName = new TreeSet<>();

// Adds an element (duplicates are ignored).
set.add(element);

// Checks whether the set contains an element.
set.contains(element);

// Removes an element.
set.remove(element);

Method List

Method / ClassDescription
new HashSet<>()Creates a hash table-based set. Element order is not guaranteed.
new LinkedHashSet<>()Creates a set that maintains insertion order.
new TreeSet<>()Creates a set that keeps elements in natural (ascending) order.
add(E e)Adds the specified element. Returns false if the element already exists.
contains(Object o)Checks whether the set contains the specified element, and returns a boolean.
remove(Object o)Removes the specified element. Returns true if the element was successfully removed.

Sample Code

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;

// Creates a HashSet and adds elements.
Set<String> fruits = new HashSet<>();
fruits.add("apple");
fruits.add("banana");
fruits.add("cherry");
fruits.add("apple"); // Duplicate is ignored.
System.out.println(fruits.size()); // Prints "3".

// Checks whether elements exist.
System.out.println(fruits.contains("banana")); // Prints "true".
System.out.println(fruits.contains("grape"));  // Prints "false".

// Removes an element.
fruits.remove("banana");
System.out.println(fruits.contains("banana")); // Prints "false".

// LinkedHashSet maintains insertion order.
Set<String> ordered = new LinkedHashSet<>();
ordered.add("banana");
ordered.add("apple");
ordered.add("cherry");
System.out.println(ordered); // Prints "[banana, apple, cherry]".

// TreeSet keeps elements in natural (ascending) order.
Set<String> sorted = new TreeSet<>();
sorted.add("banana");
sorted.add("apple");
sorted.add("cherry");
System.out.println(sorted); // Prints "[apple, banana, cherry]".

Overview

HashSet is a collection that automatically excludes duplicate elements. Element order is not guaranteed, so use LinkedHashSet or TreeSet when order matters.

add() adds an element and returns false if a duplicate is added. Unlike the linear search (O(n)) of a list, contains() uses a hash table and provides O(1) lookup performance.

For retrieving the set size and iterating over elements, see size() / isEmpty() / iterator().

If you find any errors or copyright issues, please .