Java 如何获得一个流的片断

Java 如何获得一个流的片断

一个流是一个支持各种方法的对象序列,这些方法可以通过流水线产生所需的结果。流的片断是指从原始的流中,以特定的限度存在的元素流。

例子。

输入。[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

输出:[15, 16, 17, 18, 19]

解释。输出包含从索引4到8的流的一个片断。

输入。[1, 2, 3, 4, 5, 6, 7, 8, 9]

输出:[2, 3, 4]

说明。输出包含从索引1到3的流的一个片断。

下面是在Java中从List中删除空值的方法。

  1. 使用skip()和limit()。Java中的流API提供了skip()方法,用于丢弃流中的其他非必需元素。它还提供了limit()函数,用于以指定的索引为界限,按遇到的顺序获取新的流。

步骤:

  1. 获取要切分的流。
  2. 从流中获取要分片的From和To索引作为StartIndexEndIndex
  3. 调用skip()方法,指定在起始索引之前要跳过的元素数量为skip(startIndex)
  4. 调用limit()方法来指定从流中被限制的元素数量,即limit(endIndex – startIndex + 1)
  5. 返回切分的流
    // Java program to get slice of a stream using
    // Stream skip() and limit()
    import java.util.*;
    import java.util.stream.Stream;
      
    class GFG {
      
        // Generic function to get Slice of a
        // Stream from startIndex to endIndex
        public static <T> Stream<T>
        getSliceOfStream(Stream<T> stream, int startIndex, 
                                              int endIndex)
        {
            return stream
                // specify the number of elements to skip
                .skip(startIndex)
      
                // specify the no. of elements the stream
                // that should be limited
                .limit(endIndex - startIndex + 1);
        }
        public static void main(String[] args)
        {
      
            // Create a new List with values 11 - 20
            List<Integer> list = new ArrayList<>();
            for (int i = 11; i <= 20; i++)
                list.add(i);
      
            // Create stream from list
            Stream<Integer> intStream = list.stream();
      
            // Print the stream
            System.out.println("List: " + list);
      
            // Get Slice of Stream
            // containing of elements from the 4th index to 8th
            Stream<Integer>
                sliceOfIntStream = getSliceOfStream(intStream, 4, 8);
      
            // Print the slice
            System.out.println("\nSlice of Stream:");
            sliceOfIntStream.forEach(System.out::println);
        }
    }

输出:

List: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Slice of Stream:
15
16
17
18
19
  1. 使用收集器与skip()和limit()一起。在这个方法中,流被转换为列表,然后使用收集器的一个函数来获取所需元素的子列表,并使用stream.collectors.collectAndThen()将子列表的id转换回流。

步骤:

  1. 获取要切分的流。
  2. 从流中获取要分片的From和To索引作为StartIndexEndIndex
  3. 使用Collectors.collectionAndThen ,
  4. 使用Collectors.toList()将Stream转换为List。
  5. 从列表中获取流,如list.stream()
  6. 调用skip()方法来指定在起始索引之前要跳过的元素数量,如skip(startIndex)
  7. 调用limit()方法来指定流中的元素数量,limit(endIndex – startIndex + 1)应被限制。
  8. 使用stream.collect()收集切片的列表流。
  9. 返回切分的流
// Java program to get slice of a stream using
// Collection skip() and limit()
import java.util.*;
import java.util.stream.*;
  
class GFG {
  
    // Generic function to get Slice of a
    // Stream from startIndex to endIndex
    public static <T> Stream<T>
    getSliceOfStream(Stream<T> stream, int startIndex, int endIndex)
    {
        return stream.collect(Collectors.collectingAndThen(
  
            // 1st argument
            // Convert the stream to list
            Collectors.toList(),
  
            // 2nd argument
            list -> list.stream()
                        // specify the number of elements to skip
                        .skip(startIndex)
  
                        // specify the no. of elements the stream
                        // that should be limited
                        .limit(endIndex - startIndex + 1)));
    }
    public static void main(String[] args)
    {
  
        // Create a new List with values 11 - 20
        List<Integer> list = new ArrayList<>();
        for (int i = 11; i <= 20; i++)
            list.add(i);
  
        // Create stream from list
        Stream<Integer> intStream = list.stream();
  
        // Print the stream
        System.out.println("List: " + list);
  
        // Get Slice of Stream
        // containing of elements from the 4th index to 8th
        Stream<Integer>
            sliceOfIntStream = getSliceOfStream(intStream, 4, 8);
  
        // Print the slice
        System.out.println("\nSlice of Stream:");
        sliceOfIntStream.forEach(System.out::println);
    }
}

输出:

List: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Slice of Stream:
15
16
17
18
19
  1. 获取一个子列表:该方法涉及到将一个流转换为一个列表。现在这个列表被用来在指定的索引之间获取一个所需的子列表。最后,这个子列表被转换回流。

步骤:

  1. 获取要切分的流。
  2. 从流中获取要分片的From和To索引作为StartIndexEndIndex
  3. 使用Collectors.toList()将Stream转换为List,然后使用stream.collect()收集它。
  4. 使用subList(startIndex, endIndex + 1)从收集的List中获取subList,以startIndex和endIndex+1为限。
  5. 使用stream()将子列表转换为流。
  6. 返回切分的流
// Java program to get slice of a stream by
// fetching a sublist
import java.util.*;
import java.util.stream.*;
  
class GFG {
  
    // Generic function to get Slice of a
    // Stream from startIndex to endIndex
    public static <T> Stream<T>
    getSliceOfStream(Stream<T> stream, int startIndex, int endIndex)
    {
        return stream
            // Convert the stream to list
            .collect(Collectors.toList())
  
            // Fetch the subList between the specified index
            .subList(startIndex, endIndex + 1)
  
            // Convert the subList to stream
            .stream();
    }
  
    public static void main(String[] args)
    {
  
        // Create a new List with values 11 - 20
        List<Integer> list = new ArrayList<>();
        for (int i = 11; i <= 20; i++)
            list.add(i);
  
        // Create stream from list
        Stream<Integer> intStream = list.stream();
  
        // Print the stream
        System.out.println("List: " + list);
  
        // Get Slice of Stream
        // containing of elements from the 4th index to 8th
        Stream<Integer>
            sliceOfIntStream = getSliceOfStream(intStream, 4, 8);
  
        // Print the slice
        System.out.println("\nSlice of Stream:");
        sliceOfIntStream.forEach(System.out::println);
    }
}

输出:

List: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Slice of Stream:
15
16
17
18
19

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程