假设我有一个自定义对象列表MaDate
,其字段类型为temp
的字段int
。在第一个达到某个阈值MaDate.temp >= 10
之后,我想使用流获取所有项目。
class MaDate {
int temp;
// some other fields
MaDate(int temp){
this.temp = temp;
}
int getTemp(){
return temp;
}
}
和
List<MaDate> myList = new ArrayList<>();
myList.add(new MaDate(3));
myList.add(new MaDate(7));
myList.add(new MaDate(8));
myList.add(new MaDate(4));
myList.add(new MaDate(10));
myList.add(new MaDate(3));
myList.add(new MaDate(9));
理想情况下,结果列表应包含具有temp
值[10,3,9]的最后3个元素。
我不能使用filter
myList.stream().filter(m -> m.getTemp() >= 10)...
因为这将消除值小于10的每个对象,而且我也不能使用skip
myList.stream().skip(4)...
因为我事先不知道索引。我不能使用
findFirst(m -> m.getTemp() >= 10)
因为一旦触及阈值后,我便需要所有对象,
我可以将上述内容以某种方式组合以获取所需的内容,还是编写自己的方法放入skip
或filter
myList.stream().skip(**as long as treshold not met**)
或
myList.stream().filter(**all elements after first element value above 10**)
?