Java LinkedHashSet add() Method

The LinkedHashSet.add() method in Java is used to add elements to a LinkedHashSet.

Table of Contents

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

Introduction

The LinkedHashSet.add() method is a member of the LinkedHashSet class in Java. It allows you to add elements to a LinkedHashSet. 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 LinkedHashSet.
  • 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 LinkedHashSet

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

Example

import java.util.LinkedHashSet;

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

        // Adding elements to the LinkedHashSet
        animals.add("Lion");
        animals.add("Tiger");
        animals.add("Elephant");

        // Printing the LinkedHashSet
        System.out.println("LinkedHashSet: " + animals);
    }
}

Output:

LinkedHashSet: [Lion, Tiger, Elephant]

Handling Duplicate Elements

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

Example

import java.util.LinkedHashSet;

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

        // Adding elements to the LinkedHashSet
        boolean added1 = animals.add("Lion");
        boolean added2 = animals.add("Tiger");
        boolean added3 = animals.add("Lion"); // Attempt to add a duplicate

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

        // Printing the LinkedHashSet
        System.out.println("LinkedHashSet: " + animals);
    }
}

Output:

Added Lion first time: true
Added Tiger: true
Added Lion second time (duplicate): false
LinkedHashSet: [Lion, Tiger]

Conclusion

The LinkedHashSet.add() method in Java provides a way to add elements to a LinkedHashSet. 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