I'm working on a mission.
Regarding this mission, the reviewer asked me to change the form to stream, how can I solve this?
public int next(int memberIndex) {
int result = 0;
for(int i = 0; i < last(); i++){
memberIndex = current(i).next(memberIndex);
result = memberIndex;
}
return result;
}
The method I thought was as below, but in this case, memberIndex was not updated, so I couldn't get the correct result value.
public int next(int memberIndex) {
return IntStream.range(0, last())
.mapToObj((index) -> current(index)
.next(memberIndex))
.findFirst()
.orElseThrow(IllegalArgumentException::new);
}
current() and last() methods are shown below.
private Line current(int index) {
return this.entireLine()
.get(index);
}
public int last() {
return this.entireLine()
.size();
}
How can we solve this?
memberIdx = atomicInteger(memberIndex); this.entireLine.forEach(line -> memberIdx.set(line.next(memberIdx.get()));