0

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?

4
  • 1
    Maybe don't try to build the same logic but try to achieve the same result. What is that code meant to do? Commented May 11, 2021 at 15:20
  • @Thomas Logic to check the result by looping the number of members. MemberIndex needs to be updated continuously to compare the next steps, but I'm not sure how to handle it as a stream. Commented May 11, 2021 at 15:23
  • Are these the only details that were given? Otherwise, some assumptions may need to be made. Commented May 11, 2021 at 15:43
  • @WJS Yes, it was just a matter of changing the source, and we got a hint in the answer below, and we're going to use AtomicInteger memberIdx = atomicInteger(memberIndex); this.entireLine.forEach(line -> memberIdx.set(line.next(memberIdx.get())); Commented May 11, 2021 at 15:45

1 Answer 1

1

What you are looking for is the stream member of the ArrayList.

Using stream on your this.entireLine which I assume is a List<Line>, you can loop over all the elements and perform you logic instead of using a traditional for loop

public int next(int memberIndex) {
    int result = 0;
    this.entireLine.stream().forEach(line -> {
        memberIndex = line.next(memberIndex);
        result = memberIndex;
    });
    return result;
}

Is this what you wanted ?

For more on Streams, check this tutorial out.

Sign up to request clarification or add additional context in comments.

3 Comments

I also tried to handle it similarly, but inside Lambda expression, it was impossible to change the variable because only the final value was processed. How can I solve this problem? To solve this problem, we processed the result with AtomicInteger and made it return result.get() at the end, and I wonder if it is the right way.
Yes, using Atomic is one to solve the problem and it is definitely acceptable.
Well, using AtomicInteger will solve the effective final problem. Whether it's acceptable is up to the reviewer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.