當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java IntStream boxed()用法及代碼示例


IntStream boxed()返回一個包含此流元素的流,每個元素都裝在整數中。

注意:IntStream boxed()是中間操作。這些操作總是很懶。在Stream實例上調用中間操作,並在完成處理後將中間實例作為輸出提供。
用法:

Stream<Integer> boxed()

參數:


  1. Stream : 支持順序和並行聚合操作的一係列元素。
  2. Integer : Integer類將原始類型int的值包裝在對象中。類型為Integer的對象包含一個類型為int的字段。

返回值:該函數將裝箱的流返回到整數。

示例1:

// Java code for IntStream boxed() 
import java.util.*; 
import java.util.stream.Stream; 
import java.util.stream.IntStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an IntStream 
        IntStream stream = IntStream.range(3, 8); 
  
        // Creating a Stream of Integers 
        // Using IntStream boxed() to return 
        // a Stream consisting of the elements 
        // of this stream, each boxed to an Integer. 
        Stream<Integer> stream1 = stream.boxed(); 
  
        // Displaying the elements 
        stream1.forEach(System.out::println); 
    } 
}

輸出:

3
4
5
6
7

示例2:

// Java code for IntStream boxed() 
import java.util.*; 
import java.util.stream.Stream; 
import java.util.stream.IntStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an IntStream 
        IntStream stream = IntStream.range(3, 8); 
  
        // Creating a Stream of Integers 
        // Using IntStream boxed() to return 
        // a Stream consisting of the elements 
        // of this stream, each boxed to an Integer. 
        Stream<Integer> stream1 = stream.boxed(); 
  
        Stream<Object> stream2 = Stream.concat(stream1, 
                                    Stream.of("Geeks", "for", "Geeks")); 
  
        // Displaying the elements 
        stream2.forEach(System.out::println); 
    } 
}

輸出:

3
4
5
6
7
Geeks
for
Geeks


相關用法


注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 IntStream boxed() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。