LinkedHashSet stream() in Java

stream(): This method is available in java.util.LinkedHashSet 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.LinkedHashSet;
import java.util.stream.Stream;

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

        LinkedHashSet<String> linkedHashSet =
new LinkedHashSet<>();

        linkedHashSet.add("Hello");
        linkedHashSet.add("Java");
        Stream<String> stream = linkedHashSet.stream();

        System.out.println(stream.count());

    }
}

Output:

2


No comments:

Post a Comment