Stream builder() in Java with Examples

Stream builder() returns a builder for a Stream.

Syntax :

static <T> Stream.Builder<T> builder()

where, T is the type of elements.

Return Value : A stream builder.



Example 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for Stream builder()
import java.util.*;
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Using Stream builder()
        Stream.Builder<String> builder = Stream.builder();
  
        // Adding elements in the stream of Strings
        Stream<String> stream = builder.add("Geeks").build();
  
        // Displaying the elements in the stream
        stream.forEach(System.out::println);
    }
}

chevron_right


Output :

Geeks

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for Stream builder()
import java.util.*;
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Using Stream builder()
        Stream.Builder<String> builder = Stream.builder();
  
        // Adding elements in the stream of Strings
        Stream<String> stream = builder.add("Geeks")
                                    .add("for")
                                    .add("Geeks")
                                    .add("GeeksQuiz")
                                    .build();
  
        // Displaying the elements in the stream
        stream.forEach(System.out::println);
    }
}

chevron_right


Output :

Geeks
for
Geeks
GeeksQuiz

Example 3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for Stream builder()
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.Collectors;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Using Stream builder()
        Stream.Builder<String> builder = Stream.builder();
  
        // Adding elements in the stream of Strings
        Stream<String> stream = builder.add("GEEKS")
                                    .add("for")
                                    .add("Geeks")
                                    .add("GeEKSQuiz")
                                    .build();
  
        // Converting elements to Lower Case
        // and storing them in List list
        List<String> list = stream.map(String::toLowerCase)
                                .collect(Collectors.toList());
  
        // Displaying the elements in list
        System.out.println(list);
    }
}

chevron_right


Output :

[geeks, for, geeks, geeksquiz]


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.