A. 流动 是一系列支持各种方法的对象,这些方法可以通过流水线生成所需的结果。 小河 指从原始流中以指定限制存在的元素流。
null
例如:
输入: [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中从列表中删除空值的方法:
- 使用skip()和limit() : Java中的流API 提供 跳过() 方法,用于从流中丢弃其他非必需元素。它还提供 限制() 函数,用于按遇到的顺序获取指定索引为限制的新流。
算法 :
- 让溪流切片。
- 获取要从流中切片的From和To索引,如下所示: StartIndex 和 EndIndex
- 调用skip()方法指定在开始索引之前要跳过的元素数 跳过(startIndex)
- 调用limit()方法指定流中应限制为 限制(endIndex–startIndex+1)
- 归还 断流
// 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(
"Slice 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
- 将收集器与skip()和limit()一起使用 :在这种方法中,流被转换为列表,然后使用收集器的函数来获取所需元素的子列表,子列表id使用 流动collect(Collectors.collectingAndThen()) .
算法 :
- 让溪流切片。
- 获取要从流中切片的From和To索引,如下所示: StartIndex 和 EndIndex
- 使用 收藏家。收集然后 ,
- 使用以下命令将流转换为列表 收藏家。托利斯特()
- 从列表中获取流,如下所示: 列表流()
- 调用skip()方法指定在开始索引之前要跳过的元素数 跳过(startIndex)
- 调用limit()方法指定流中应限制为 限制(endIndex–startIndex+1)
- 使用 流动收集
- 归还 断流
// 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(
"Slice 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
- 获取子列表 :此方法涉及将流转换为列表。现在,该列表用于从指定索引之间的列表中获取所需的子列表。最后,这个子列表被转换回Stream。
算法 :
- 让溪流切片。
- 获取要从流中切片的From和To索引,如下所示: StartIndex 和 EndIndex
- 使用以下命令将流转换为列表 收藏家。托利斯特() 然后使用 流动收集
- 从收集的列表中获取子列表,使用startIndex和endIndex+1作为限制 子列表(startIndex,endIndex+1)
- 使用以下命令将子列表转换回流 流()
- 归还 断流
// 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(
"Slice 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
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END