Java Stream How to - Convert Set to Stream








Question

We would like to know how to convert Set to Stream.

Answer

//  w  w w. j a va2  s  .co  m
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;

public class Main {
   public static void main(String[] args) {
     Set<Integer> numberSet = new HashSet<Integer>() {{
       add(1);
       add(2);
       add(3);
    }};
    Stream<Integer> collectionStream = numberSet.stream();


     System.out.println(Arrays.toString(collectionStream.toArray()));
   }

}

The code above generates the following result.