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


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


LongStream forEachOrdered(LongConsumer action)以遇到順序對此流的每個元素執行一個動作。 LongStream forEachOrdered(LongConsumer action)是一種終端操作,即,它可以遍曆流以產生結果或副作用。

用法:

void forEachOrdered(LongConsumer action)

參數:LongConsumer表示一個接受單個long值參數且不返回結果的操作。長期以來,這是Consumer的原始類型專業化。


注意:如果流具有已定義的遇到順序,則forEachOrdered(LongConsumer action)以該流的遇到順序對此流的每個元素執行一個動作。

示例1:

// Java code for LongStream forEachOrdered 
// (LongConsumer action) in Java 8 
import java.util.*; 
import java.util.stream.LongStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an LongStream 
        LongStream stream = LongStream.of(2L, 3L, 4L, 5L); 
  
        // Using LongStream.forEachOrdered 
        stream.forEachOrdered(System.out::println); 
    } 
}
輸出:
2
3
4
5

示例2:

// Java code for LongStream forEachOrdered 
// (LongConsumer action) in Java 8 
import java.util.*; 
import java.util.stream.LongStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an LongStream 
        LongStream stream = LongStream.range(5L, 11L); 
  
        // Using LongStream.forEachOrdered() on 
        // sequential stream 
        stream.forEachOrdered(System.out::println); 
    } 
}
輸出:
5
6
7
8
9
10

示例3:

// Java code for LongStream forEachOrdered 
// (LongConsumer action) in Java 8 
import java.util.*; 
import java.util.stream.LongStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an LongStream 
        LongStream stream = LongStream.range(5, 11); 
  
        // Using LongStream.forEachOrdered() on 
        // parallel stream 
        stream.parallel().forEachOrdered(System.out::println); 
    } 
}

輸出:

5
6
7
8
9
10


相關用法


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