Conversion of Set To Stream in Java

Being a type of Collection, we can convert a set to Stream using its stream() method.

Example

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

public class Tester {
    public static void main(String args[]) {
        Set<String> set = new HashSet<String>();

        set.add("a");
        set.add("b");
        set.add("c");
        set.add("d");
        set.add("e");
        set.add("f");

        Stream<String> stream = set.stream();
        stream.forEach(data->System.out.print(data+" "));
    }
}

Output

a b c d e f
Updated on: 2020-06-18T15:31:03+05:30

997 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements