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


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


長流boxed()返回一個由該流的元素組成的Stream,每個元素都裝箱成Long。
注意:LongStream boxed()是中間操作。這些操作總是很懶。在Stream實例上調用中間操作,並在完成處理後將中間實例作為輸出提供。
用法:

Stream<Long> boxed()

參數:

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

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


示例1:

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

輸出:

3
4
5
6
7

示例2:

// Java code for LongStream boxed() 
import java.util.*; 
import java.util.stream.Stream; 
import java.util.stream.LongStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an LongStream 
        LongStream stream = LongStream.range(3L, 8L); 
  
        // Creating a Stream of Longs 
        // Using LongStream boxed() to return 
        // a Stream consisting of the elements 
        // of this stream, each boxed to a Long. 
        Stream<Long> 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大神的英文原創作品 LongStream boxed() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。