Java Streams - Stream.Builder add(T t) example








Stream.Builder add(T t) adds an element to the stream being built.

Syntax

add has the following syntax.

default Stream.Builder<T> add(T t)

Example

The following example shows how to use add.

import java.util.stream.Stream;
//from ww  w .java2s.  com
public class Main {
  public static void main(String[] args) {
    Stream.Builder<String> b = Stream.builder();
    b.add("a");
    b.add("b");
    b.add("c");
    b.add("d");
    b.add("e");
    
    Stream<String> s = b.build();
    s.forEach(System.out::println); 

  }
}

The code above generates the following result.