Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Checking, adding, and removing set elements in Java

Check whether an element belongs to a set and tell whether an addition or removal changed it. Use `LinkedHashSet` to track completed lessons without duplicates and in insertion order.

Version: Java SE 21

Syntax

boolean contains(Object o);
boolean add(E e);
boolean remove(Object o);

The Set.contains/add/remove methods work with individual elements in a java.util.Set<E>, a collection with no duplicates. contains(Object o) leaves the set unchanged: it returns true if a matching element is present and false otherwise. The Set contract defines a match using Objects.equals, not reference comparison with ==.

add(E e) takes an element of type E. It returns true when it inserts the element, or false when an equal element is already present and the set stays unchanged. remove(Object o) removes a matching element, not an element at an index. It returns true when it removes the element and false when no match is present. Both results tell you whether the set changed, so there is no need to call contains first.

add and remove are optional operations. If the set does not support the operation, the call can throw UnsupportedOperationException. An Object parameter does not mean every argument is accepted: depending on the implementation, contains or remove with an incompatible type or a disallowed null may return false or throw ClassCastException or NullPointerException, respectively. add can also reject an element because of one of its properties by throwing IllegalArgumentException.

LinkedHashSet supports both mutations and permits null. With ordinary add, it iterates in insertion order: adding an element that is still present does not move it, but removing it and then adding it again puts it last. This ordering is a guarantee of LinkedHashSet, not of every Set.

Examples

import java.util.LinkedHashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set<String> completed = new LinkedHashSet<>();
        System.out.println("add lesson-22: " + completed.add("lesson-22"));
        completed.add("lesson-23");
        System.out.println("add lesson-22 again: " + completed.add("lesson-22"));
        System.out.println("order: " + String.join(", ", completed));
        System.out.println("contains lesson-22: " + completed.contains("lesson-22"));
        System.out.println("remove lesson-22: " + completed.remove("lesson-22"));
        System.out.println("remove lesson-22 again: " + completed.remove("lesson-22"));
        System.out.println("contains lesson-22: " + completed.contains("lesson-22"));
        System.out.println("add lesson-22 after removal: " + completed.add("lesson-22"));
        System.out.println("order: " + String.join(", ", completed));
    }
}

Result

add lesson-22: true
add lesson-22 again: false
order: lesson-22, lesson-23
contains lesson-22: true
remove lesson-22: true
remove lesson-22 again: false
contains lesson-22: false
add lesson-22 after removal: true
order: lesson-23, lesson-22

An ID in the set marks a lesson as completed. Adding `lesson-22` while it is still present changes nothing; removing and then adding it again places it after `lesson-23`.

Related lessons

  • Java · Lesson 22 · Intermediate · Version: 2

Sources