Skip to content
Related Articles

Related Articles

Stream builder() in Java with Examples
  • Last Updated : 06 Dec, 2018

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 :




// 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);
    }
}


Output :



Geeks

Example 2 :




// 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);
    }
}


Output :

Geeks
for
Geeks
GeeksQuiz

Example 3 :




// 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);
    }
}


Output :

[geeks, for, geeks, geeksquiz]

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :