Java LinkedHashSet stream() Method

The LinkedHashSet.stream() method in Java is used to create a sequential stream over the elements in a LinkedHashSet.

Table of Contents

  1. Introduction
  2. stream Method Syntax
  3. Examples
    • Creating a Stream from a LinkedHashSet
    • Processing Elements Using Stream API
  4. Conclusion

Introduction

The LinkedHashSet.stream() method is a member of the LinkedHashSet class in Java. It allows you to create a sequential stream over the elements in the LinkedHashSet, enabling various stream operations like filtering, mapping, and collecting.

stream() Method Syntax

The syntax for the stream method is as follows:

public Stream<E> stream()
  • The method does not take any parameters.
  • The method returns a Stream over the elements in the LinkedHashSet.

Examples

Creating a Stream from a LinkedHashSet

The stream method can be used to create a sequential stream from a LinkedHashSet.

Example

import java.util.LinkedHashSet;
import java.util.stream.Stream;

public class StreamExample {
    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");

        // Creating a stream from the LinkedHashSet
        Stream<String> stream = animals.stream();

        // Printing the elements of the stream
        stream.forEach(animal -> System.out.println("Animal: " + animal));
    }
}

Output:

Animal: Lion
Animal: Tiger
Animal: Elephant

Processing Elements Using Stream API

Streams can be used to process elements in various ways, such as filtering, mapping, and collecting.

Example

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamProcessingExample {
    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");
        animals.add("Giraffe");
        animals.add("Zebra");

        // Creating a stream from the LinkedHashSet
        Stream<String> stream = animals.stream();

        // Collecting the elements that start with the letter 'E' in a new Set
        Set<String> filteredAnimals = stream
            .filter(animal -> animal.startsWith("E"))
            .collect(Collectors.toSet());

        // Printing the elements of the filteredAnimals Set
        filteredAnimals.forEach(animal -> System.out.println("Animal: " + animal));
    }
}

Output:

Animal: Elephant

Conclusion

The LinkedHashSet.stream() method in Java provides a way to create a sequential stream over the elements in a LinkedHashSet. By understanding how to use this method, you can leverage the Stream API to perform various operations on collections, such as filtering, mapping, and collecting. This method is useful for processing collections in a functional style, making it a valuable tool for managing data in your Java applications.

Comments