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


Java stream.limit()用法及代码示例


先决条件: Java8中的流
limit(long N)是java.util.stream.Stream对象的方法。此方法以一个(长N)作为参数,并返回大小不超过N的流。如果N的值很大,则limit()在有序并行管道上可能会非常昂贵,因为limit(N)受限于返回遇到顺序中的前N个元素,而不仅仅是任何n个元素。

用法:

Stream<T> limit(long N)

Where N is the number of elements the stream should be 
limited to and this function returns new stream as output.

异常:如果N的值为负,则该函数将引发IllegalArgumentException。


下面是一些示例,可以更好地理解该函数的实现。
示例1:

// Java code to show implementation 
// of limit() function 
import java.util.*; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
  
        // Creating a list of integers 
        List<Integer> list = new ArrayList<Integer>(); 
  
        // adding elements in the list 
        list.add(-2); 
        list.add(0); 
        list.add(2); 
        list.add(4); 
        list.add(6); 
        list.add(8); 
        list.add(10); 
        list.add(12); 
        list.add(14); 
        list.add(16); 
  
        // setting the value of N as 4 
        int limit = 4; 
        int count = 0; 
        Iterator<Integer> it = list.iterator(); 
  
        // Iterating through the list of integers 
        while (it.hasNext()) { 
            it.next(); 
            count++; 
  
            // Check if first four i.e, (equal to N) 
            // integers are iterated. 
            if (count > limit) { 
  
                // If yes then remove all the remaining integers. 
                it.remove(); 
            } 
        } 
  
        System.out.print("New stream of length N"
                         + " after truncation is : "); 
  
        // Displaying new stream of length 
        // N after truncation 
        for (Integer number : list) { 
            System.out.print(number + " "); 
        } 
    } 
}

输出:

New stream of length N after truncation is : -2 0 2 4 

应用:

// Java code to show the use of limit() function 
import java.util.stream.Stream; 
import java.util.ArrayList; 
import java.util.List; 
 class gfg{ 
       
     // Function to limit the stream upto given range, i.e, 3 
     public static Stream<String> limiting_func(Stream<String> ss, int range){ 
         return ss.limit(range); 
     } 
       
     // Driver code 
     public static void main(String[] args){ 
           
         // list to save stream of strings 
         List<String> arr = new ArrayList<>(); 
           
         arr.add("geeks"); 
         arr.add("for"); 
         arr.add("geeks"); 
         arr.add("computer"); 
         arr.add("science"); 
          
         Stream<String> str = arr.stream(); 
           
         // calling function to limit the stream to range 3 
         Stream<String> lm = limiting_func(str,3); 
         lm.forEach(System.out::println); 
     } 
 }

输出:

geeks
for
geeks


相关用法


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