Java HashSet.add() – Examples

In this tutorial, we will learn about the Java HashSet.add() method, and learn how to use this method to add an element to this HashSet, with the help of examples.

add(element)

HashSet.add() adds the specified element to this HashSet if it is not already present.

Since HashSet stores only unique elements, if we try to add an element that is already present, add() method returns false.

If the element is not present, add() method adds the element to the HashSet and returns true.

ADVERTISEMENT

Syntax

The syntax of add() method is

HashSet.add(E e)

where

ParameterDescription
eThe element that has to be added to the HashSet.

Returns

The method returns boolean value of true or false.

Example 1 – add(element)

In this example, we will create and initialize a HashSet of Strings with some elements. We will then add a new element e to this HashSet using add() method. We will take the value of e such that it is not already present in the HashSet. add() method should add the element to the list and return true.

Java Program

import java.util.Arrays;
import java.util.HashSet;

public class Example {  
	public static void main(String[] args) {  
		HashSet<String> hashSet = new HashSet<String>(Arrays.asList("a", "b", "c"));
		System.out.println("Original HashSet  : " + hashSet);
		
		//add element to hashSet
		String e = "d";
		boolean result = hashSet.add(e);
		System.out.println("Is the new element added to the HashSet? " + result);
		System.out.println("HashSet after add : " + hashSet);
    }  
}

Output

Original HashSet  : [a, b, c]
Is the new element added to the HashSet? true
HashSet after add : [a, b, c, d]

Example 2 – add(element) – Element already present

In this example, we will try to add an element e to the HashSet that is alredy present. add() should return a boolean value of false.

Java Program

import java.util.Arrays;
import java.util.HashSet;

public class Example {  
	public static void main(String[] args) {  
		HashSet<String> hashSet = new HashSet<String>(Arrays.asList("a", "b", "c"));
		System.out.println("Original HashSet  : " + hashSet);
		
		//add element to hashSet
		String e = "a";
		boolean result = hashSet.add(e);
		System.out.println("Is the new element added to the HashSet? " + result);
		System.out.println("HashSet after add : " + hashSet);
    }  
}

Output

Original HashSet  : [a, b, c]
Is the new element added to the HashSet? false
HashSet after add : [a, b, c]

Example 3 – add(element) – User Defined Object

In this example, we will create a HashSet that can store elements of user defined class Car, and add elements to this HashSet.

Java Program

import java.util.HashSet;

public class Example {  
	public static void main(String[] args) {
		HashSet<Car> hashSet = new HashSet<Car>();
		hashSet.add(new Car(1, "Tesla"));
		hashSet.add(new Car(2, "BMW"));
		hashSet.add(new Car(3, "Toyota"));
		
		System.out.println(hashSet);
    }  
}

class Car {
	int id;
	String name;
	public Car(int id, String name) {
		this.id = id;
		this.name = name;
	}
	
	@Override
	public String toString() {
		return id + "-" + name;
	}
}

Output

[1-Tesla, 3-Toyota, 2-BMW]

Example 4 – add(element) – Null HashSet

In this example, we will initialize a HashSet with null. If we call add() method on this null object, the method would throw java.lang.NullPointerException.

Java Program

import java.util.HashSet;

public class Example {  
	public static void main(String[] args) {  
		HashSet<String> hashSet = null;
		hashSet.add("a");
    }  
}

Output

Exception in thread "main" java.lang.NullPointerException
	at Example.main(Example.java:6)

Conclusion

In this Java Tutorial, we have learnt the syntax of Java HashSet.add() method, and also learnt how to use this method with the help of example programs.