Java Collection Tutorial - Java Set.add(E e)


Set.add(E e) has the following syntax.

boolean add(E e)


In the following code shows how to use Set.add(E e) method.

// w w w .  j  a  v  a 2s. c  o m
import java.util.HashSet;
import java.util.Set;

public class Main {

  public static void main(String[] a) {
    Set<String> set = new HashSet<String>();
    if (set.add("Hello")) {
      System.out.println("addition successful");
    } else {
      System.out.println("addition failed");

The code above generates the following result.