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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。