Java LinkedHashSet contains() Method

The LinkedHashSet.contains() method in Java is used to check if the LinkedHashSet contains a specified element.

Table of Contents

  1. Introduction
  2. contains Method Syntax
  3. Examples
    • Checking if an Element is Present in LinkedHashSet
    • Handling Non-Present Elements
  4. Real-World Use Case
    • Use Case: Inventory Management System
  5. Conclusion

Introduction

The LinkedHashSet.contains() method is a member of the LinkedHashSet class in Java. It allows you to check if the LinkedHashSet contains a specified element. This method is useful for verifying the presence of elements in a set.

contains() Method Syntax

The syntax for the contains method is as follows:

public boolean contains(Object o)
  • The method takes a single parameter o of type Object, which represents the element to be checked for containment in the LinkedHashSet.
  • The method returns a boolean value:
    • true if the LinkedHashSet contains the specified element.
    • false if the LinkedHashSet does not contain the specified element.

Examples

Checking if an Element is Present in LinkedHashSet

The contains method can be used to check if a specified element is present in the LinkedHashSet.

Example

import java.util.LinkedHashSet;
import java.util.Set;

public class ContainsExample {
    public static void main(String[] args) {
        // Creating a LinkedHashSet of Strings using the Set interface as reference type
        Set<String> fruits = new LinkedHashSet<>();

        // Adding elements to the LinkedHashSet
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Checking if the LinkedHashSet contains "Banana"
        boolean containsBanana = fruits.contains("Banana");

        // Printing the result
        System.out.println("Does the LinkedHashSet contain 'Banana'? " + containsBanana);
    }
}

Output:

Does the LinkedHashSet contain 'Banana'? true

Handling Non-Present Elements

The contains method returns false if the specified element is not present in the LinkedHashSet.

Example

import java.util.LinkedHashSet;
import java.util.Set;

public class ContainsNonPresentExample {
    public static void main(String[] args) {
        // Creating a LinkedHashSet of Strings using the Set interface as reference type
        Set<String> fruits = new LinkedHashSet<>();

        // Adding elements to the LinkedHashSet
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Checking if the LinkedHashSet contains "Mango"
        boolean containsMango = fruits.contains("Mango");

        // Printing the result
        System.out.println("Does the LinkedHashSet contain 'Mango'? " + containsMango);
    }
}

Output:

Does the LinkedHashSet contain 'Mango'? false

Real-World Use Case

Use Case: Inventory Management System

In an inventory management system, you might need to check if certain items are available in the inventory. The contains method can be used to check if an item is present in the inventory.

Example

import java.util.LinkedHashSet;
import java.util.Set;

public class InventoryManagementSystem {
    public static void main(String[] args) {
        // Creating a LinkedHashSet to store inventory items using the Set interface as reference type
        Set<String> inventory = new LinkedHashSet<>();

        // Adding items to the inventory
        inventory.add("Apple");
        inventory.add("Banana");
        inventory.add("Cherry");

        // Checking if an item is available in the inventory
        String itemToCheck = "Apple";
        if (inventory.contains(itemToCheck)) {
            System.out.println(itemToCheck + " is available in the inventory.");
        } else {
            System.out.println(itemToCheck + " is not available in the inventory.");
        }
    }
}

Output:

Apple is available in the inventory.

Conclusion

The LinkedHashSet.contains() method in Java provides a way to check if a LinkedHashSet contains a specified element. By understanding how to use this method, you can efficiently verify the presence of elements in your collections. This method is useful for checking membership in sets, making it a valuable tool for collection management in your Java applications. The real-world use case of an inventory management system illustrates the practical application of this method in checking item availability.

Comments