How to Create and Add elements in a HashSet in Java? | HashSet Add Method Implementation in Java

The HashSet class implements the Set interface and is supported by a hash table, which is a HashMap instance. The iteration order of the set is not guaranteed, which means that the class does not ensure a constant order of elements over time. The null element is allowable in this class. The class also provides continuous time for basic operations like adding, removing, containing, and size provided that the Hash function correctly distributes the elements between the buckets. In this tutorial, we are going to create and add elements in a HashSet.

HashSet add() Method in Java

The Java.util.HashSet.add() method present in Java HashSet permits us to add specific elements into the HashSet. You can do so only if the specified adding element is not present in the HashSet or else the function returns false if it is already existing in the HashSet.

Syntax:

Hash_Set.add(Object element)

Parameters: Parameter Element belongs to the type HashSet and denotes the element to be added to the set.

Return Value: The function returns true if the element you add is not present in the HashSet or else returns false if it is already existing in the HashSet.

Read Similar Articles:

Example Program Illustrating HashSet Add Method Implementation in Java

// Java code to illustrate add()
import java.io.*;
import java.util.HashSet;

public class HashSetDemo {
public static void main(String args[])
{
// Creating an empty HashSet
HashSet<String> set = new HashSet<String>();

// Use add() method to add elements into the Set
set.add("Welcome");
set.add("To");
set.add("Geeks");
set.add("4");
set.add("Geeks");

// Displaying the HashSet
System.out.println("HashSet: " + set);
}
}

Output:

HashSet: [4, Geeks, Welcome, To]

Create and Add elements in a HashSet

Learn how to create and add elements in a Hashset by referring to the following modules

1)Creating a new HashSet of type Integer

We can create HashSet as given below.

HashSet<Integer> intset= new HashSet<>();

Only unique integers can be stored in this HashSet object.

2)Adding given element to given HashSet

We use add() method to add given element to the HashSet.

public boolean addElement(E element)

This add() function will attempt to insert an element into HashSet and will return false if the element already exists in HashSet.

Adding an Element in Java HashSet Example

import java.io.*;
import java.lang.*;
import java.util.*;

class Codechef {
    public static void main(String[] args)
        throws java.lang.Exception
    {
        // initailzing a hashset which is of type string
        HashSet<String> stringset = new HashSet<>();
        // adding elements to the hashset
        stringset.add("hello");
        stringset.add("this");
        stringset.add("is");
        stringset.add("BTechGeeks");
        // Now we add duplicate element to hashset
        boolean return_val = stringset.add("BTechGeeks");
        // if return value is false then the element is
        // already present in hashset
        // so it returns false
        if (return_val == false)
            System.out.println(
                "Duplicate element BTechGeeks is not added");
        // print the hashset
        System.out.println("Printing the Hashset : ");
        System.out.println(stringset);
    }
}

Output:

Duplicate element BTechGeeks is not added
Printing the Hashset : 
[BTechGeeks, this, is, hello]

Related Programs: