📜  如何在Java中获取流的切片

📅  最后修改于: 2022-05-13 01:55:08.254000             🧑  作者: Mango

如何在Java中获取流的切片

流是支持各种方法的对象序列,这些方法可以流水线化以产生所需的结果。流的切片是指存在于指定限制中的元素流,来自原始流。

例子:

以下是从Java列表中删除空值的方法:

  1. 使用 skip() 和 limit() : Java中的 Stream API 提供了skip()方法,用于从流中丢弃其他不需要的元素。它还提供了limit()函数,该函数用于按照遇到的顺序获取具有指定索引作为限制的新流。

    算法

    1. 获取要切片的 Stream。
    2. 获取要从 Stream 中切片的 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  Stream
        getSliceOfStream(Stream 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 list = new ArrayList<>();
            for (int i = 11; i <= 20; i++)
                list.add(i);
      
            // Create stream from list
            Stream 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
                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
    
  2. 将收集器与 skip() 和 limit() 一起使用:在此方法中,将 Stream 转换为 List,然后使用收集器的函数来获取所需元素的子列表,并将子列表 id 转换回流使用stream.collect(Collectors.collectingAndThen())

    算法

    1. 获取要切片的 Stream。
    2. 获取要从 Stream 中切片的 From 和 To 索引作为StartIndexEndIndex
    3. 使用Collectors.collectingAndThen
    4. 使用Collectors.toList()将流转换为列表
    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  Stream
        getSliceOfStream(Stream 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 list = new ArrayList<>();
            for (int i = 11; i <= 20; i++)
                list.add(i);
      
            // Create stream from list
            Stream 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
                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
    
  3. 获取子列表:此方法涉及将流转换为列表。现在此列表用于在指定索引之间从中获取所需的子列表。最后,这个 subList 被转换回 Stream。

    算法

    1. 获取要切片的 Stream。
    2. 获取要从 Stream 中切片的 From 和 To 索引作为StartIndexEndIndex
    3. 使用Collectors.toList()将 Stream 转换为 List,然后使用stream.collect()收集它
    4. 使用subList(startIndex, endIndex + 1)从收集的 List 中获取以 startIndex 和 endIndex+1 为限制的 subList
    5. 使用stream()将 subList 转换回流
    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  Stream
        getSliceOfStream(Stream 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 list = new ArrayList<>();
            for (int i = 11; i <= 20; i++)
                list.add(i);
      
            // Create stream from list
            Stream 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
                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