Java HashSet stream() Method

The HashSet.stream() method in Java is used to create a sequential Stream with the elements of the HashSet as its source. 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. stream Method Syntax
  3. Examples
    • Creating a Stream from a HashSet
    • Filtering Elements in a Stream
    • Collecting Stream Results into a List
  4. Conclusion

Introduction

The HashSet.stream() method is a member of the HashSet class in Java. It allows you to create a Stream from the elements in the HashSet. Streams provide a powerful way to perform operations on collections of data, such as filtering, mapping, and reducing.

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<E> representing a sequential stream of elements in the HashSet.

Examples

Creating a Stream from a HashSet

The stream method can be used to create a Stream from the elements in a HashSet.

Example

import java.util.HashSet;
import java.util.stream.Stream;

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

        // Creating a Stream from the HashSet
        Stream<String> stream = languages.stream();

        // Printing the elements in the Stream
        System.out.println("Elements in the Stream:");
        stream.forEach(System.out::println);
    }
}

Output:

Elements in the Stream:
Java
C
Python

Filtering Elements in a Stream

You can use the stream method to create a Stream and then apply various operations, such as filtering, on the elements.

Example

import java.util.HashSet;
import java.util.stream.Stream;

public class FilterStreamExample {
    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");
        languages.add("JavaScript");

        // Creating a Stream from the HashSet and filtering elements
        Stream<String> filteredStream = languages.stream().filter(lang -> lang.startsWith("J"));

        // Printing the filtered elements
        System.out.println("Filtered elements in the Stream:");
        filteredStream.forEach(System.out::println);
    }
}

Output:

Filtered elements in the Stream:
Java
JavaScript

Collecting Stream Results into a List

You can use the stream method to create a Stream, apply operations, and then collect the results into a list.

Example

import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

public class CollectStreamExample {
    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");
        languages.add("JavaScript");

        // Creating a Stream from the HashSet and collecting the results into a list
        List<String> filteredList = languages.stream()
                                             .filter(lang -> lang.startsWith("J"))
                                             .collect(Collectors.toList());

        // Printing the collected list
        System.out.println("Collected list from the Stream:");
        filteredList.forEach(System.out::println);
    }
}

Output:

Collected list from the Stream:
Java
JavaScript

Conclusion

The HashSet.stream() method in Java provides a way to create a sequential Stream from the elements in a HashSet. By understanding how to use this method, you can leverage the power of streams to perform various operations on collections of data, such as filtering, mapping, and collecting. Streams offer a flexible and expressive way to process data in Java, making your code more readable and maintainable.

Leave a Comment

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

Scroll to Top