问题陈述:需要从给定索引开始以循环方式迭代的对象列表,直到我们回到相同的索引为止。需要评估条件,并且应返回与该条件匹配的第一个元素的索引。
因此,这就像满足条件的项目的下一个按钮一样
班上
public class Code {
private String type;
private String description;
private boolean option;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isOption() {
return option;
}
public void setOption(boolean option) {
this.option = option;
}
@Override
public String toString() {
return "Code [type=" + type + ", description=" + description + ", option=" + option + "]";
}
}
有一个列表(名为list),该列表具有Code类的对象和我们需要从其开始的索引(名为ind)-该索引不断变化。
可以使用如下的for循环来实现
for(int i=((ind+1)%list.size());i!=ind;i=((i+1)%list.size())) {
Code code = (Code)list.get(i);
if(!code.isOption())
{
System.out.println("Index of the record matched" + i);
break;
}
}
我想使用Java 8 IntStream实现以上功能。请让我知道如何实现。
您可以创建和连接两个int流,一个从startIndex开始,另一个在startIndex结束。
int result = IntStream
.concat(
IntStream.range( startIndex, list.size() ),
IntStream.range(0, startIndex) )
.filter( i -> list.get(i).isOption() )
.findFirst()
.orElse(-1);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句