当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Stream builder()用法及代码示例


流builder()返回流的构建器。

用法:

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

where, T is the type of elements.

返回值:流构建器。


示例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]


相关用法


注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Stream builder() in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。