Java - Streams from Collections

Introduction

Collection interface stream() and parallelStream() methods can create sequential and parallel streams from a Collection, respectively.

The following code creates streams from a set of strings:

Demo

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

public class Main {
  public static void main(String[] args) {
 // Create and populate a set of strings
    Set<String> names = new HashSet<>();
    names.add("Java");
    names.add("jeff");

    // Create a sequential stream from the set
    Stream<String> sequentialStream = names.stream();
    sequentialStream.forEach(System.out::println);
    // Create a parallel stream from the set
    Stream<String> parallelStream = names.parallelStream();
    parallelStream.forEach(System.out::println);
  }/*w ww  .j  a va 2s .co  m*/
}

Result