Java Set add() addAll() Methods Tutorial

In this section, we will learn what the Set add() and addAll() methods are and how to use them in Java.

What is Java Set add() Method?

The Java Set add() method is used to add a new element to a Set object.

Note: if the element that we’re trying to add to a Set is already there, then this method won’t do anything. Because after all, a Set object doesn’t accept duplicate elements.

Java Set add() Method Syntax:

boolean add(E e)

add() Method Parameters:

The method only takes one argument and that is the element we want to add to a Set object.

add() Method Return Value:

The return value of this method is of Boolean type.

If the element was added to the target Set, then the return value of this method become true.

Otherwise, the value false will return instead.

add() Method Exceptions:

The method might throw three exceptions:

UnsupportedOperationException: We get this exception if this method is not supported by the target Set object.

ClassCastException: We get this exception if the argument element is not of the same type as the element of the target Set object.

NullPointerException: We get this exception if we put a null value as the argument of the method and the target Set object does not permit null elements.

IllegalArgumentException: We get this exception if some property of the specified element prevents it from being added to this set

Example: using Set add() method

import java.util.Set;
import java.util.HashSet; 
public class Main {

    public static void main(String[] args){
      Set<Integer> set = new HashSet<>();
      set.add(1);
      set.add(2);
      set.add(3);
      set.add(4);
      set.add(5);
      set.add(6);
      set.add(7);
    }
}

What is Java Set addAll() Method?

The Java set addAll() method is used to add the entire elements of a collection object to a Set object.

Note: if in the target collection object there’s one or more elements that are already existing in the target set method, they will be ignored and only those unique elements will be copied to the Set object.

Java Set addAll() Method Syntax:

boolean addAll(Collection<? extends E> c)

addAll() Method Parameters:

The method takes one argument and that is a reference to the collection that we want to copy its element and put them in the target Set object.

addAll() Method Return Value:

The return value of this method is of Boolean type.

If any element from the reference collection was copied into the Set object, then the return value of this method will be true. Otherwise the value false will return.

addAll() Method Exceptions:

The method throws the same set of exceptions that the add() method might throw.

Example: using Set addAll() method

import java.util.Set;
import java.util.HashSet; 
import java.util.ArrayList;
public class Main {

    public static void main(String[] args){
      Set<Integer> set = new HashSet<>();
      set.add(1);
      set.add(2);
      set.add(3);
      set.add(4);
      set.add(5);
      set.add(6);
      set.add(7);

      ArrayList<Integer> array = new ArrayList<>();
      array.add(2);
      array.add(10);

      set.addAll(array);

      for (int i: set){
         System.out.println(i);
      }
    }
}

Output:

1

2

3

4

5

6

7

10
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies