HashSet stream() in Java

stream(): This method is available in java.util.HashSet class of Java.

Syntax:

Stream<K> java.util.Collection.stream()

This method returns a sequential Stream with this collection as its source.

Parameters: NA

Returns: a sequential Stream over the elements in this collection.

Exceptions: NA

Approach

Java

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

public class HashSetstream {
    public static void main(String[] args) {

        HashSet<String> hashSet = new HashSet<String>();

        hashSet.add("Hello");
        hashSet.add("Java");

        Stream<String> stream = hashSet.stream();

        stream.forEach(n -> System.out.print(n + " "));

    }
}

Output:

Java Hello 

No comments:

Post a Comment