Java Stream builder()及实例

Java Stream builder()及实例

Stream builder() 返回一个Stream的构建器。

语法:

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

其中,T是元素的类型。

返回值: 一个流构建器。

例子 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);
    }
}

输出:

Geeks

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

输出:

Geeks
for
Geeks
GeeksQuiz

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

输出:

[geeks, for, geeks, geeksquiz]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程