Monday, November 18, 2024
Google search engine
HomeLanguagesJavaStream builder() in Java with Examples

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 :




// 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]

RELATED ARTICLES

Most Popular

Recent Comments