Java HashSet add() Method

The HashSet.add() method in Java is used to add elements to a HashSet. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. add Method Syntax
  3. Examples
    • Adding Elements to a HashSet
    • Handling Duplicate Elements
  4. Conclusion

Introduction

The HashSet.add() method is a member of the HashSet class in Java. It allows you to add elements to a HashSet. If the element is not already present in the set, it is added and the method returns true; otherwise, it returns false.

add Method Syntax

The syntax for the add method is as follows:

public boolean add(E e)
  • The method takes a single parameter e of type E, which represents the element to be added to the HashSet.
  • The method returns a boolean value:
    • true if the element was added to the set (it was not already present).
    • false if the element was not added (it was already present in the set).

Examples

Adding Elements to a HashSet

The add method can be used to add elements to a HashSet.

Example

import java.util.HashSet;

public class AddExample {
    public static void main(String[] args) {
        // Creating a HashSet of Strings
        HashSet<String> languages = new HashSet<>();

        // Adding elements to the HashSet
        languages.add("Java");
        languages.add("Python");
        languages.add("C");

        // Printing the HashSet
        System.out.println("HashSet: " + languages);
    }
}

Output:

HashSet: [Java, C, Python]

Handling Duplicate Elements

The add method returns false if the element is already present in the HashSet.

Example

import java.util.HashSet;

public class DuplicateExample {
    public static void main(String[] args) {
        // Creating a HashSet of Strings
        HashSet<String> languages = new HashSet<>();

        // Adding elements to the HashSet
        boolean added1 = languages.add("Java");
        boolean added2 = languages.add("Python");
        boolean added3 = languages.add("Java"); // Attempt to add a duplicate

        // Printing the results of adding elements
        System.out.println("Added Java first time: " + added1);
        System.out.println("Added Python: " + added2);
        System.out.println("Added Java second time (duplicate): " + added3);

        // Printing the HashSet
        System.out.println("HashSet: " + languages);
    }
}

Output:

Added Java first time: true
Added Python: true
Added Java second time (duplicate): false
HashSet: [Java, Python]

Conclusion

The HashSet.add() method in Java provides a way to add elements to a HashSet. By understanding how to use this method, you can efficiently manage collections of unique elements in your Java applications. The method ensures that no duplicate elements are added to the set, maintaining the uniqueness of the elements.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top