Java LinkedHashSet stream() Method

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

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

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare