0

I have the following lists:

List<Token> originals
List<Token> modified

The Token class is as follows:

class Token
{
    private String value;
    private String rawValue;
}

I want to replace the rawValue of all the Token objects in originals, with the rawValue of the Token object at the corresponding index in modified. Both the lists are guaranteed to be equal in length.

Here is the pseudocode:

for i in originals.length
    originals[i].rawValue = modified[i].rawValue

Is there a way to do this using Stream? Or is using a For loop simpler and more readable here?

Rizvan Ferdous
  • 69
  • 1
  • 2
  • 12
  • 4
    I would definitely use a for loop or while loop for this. If you want to use streams, you'd probably want to [zip](https://stackoverflow.com/q/17640754/276052) the streams, and work with pairs of Token objects. – aioobe Sep 09 '20 at 20:22
  • You can use an IntStream to iterate but honestly the Stream API is not designed to mutate the original Collection therefore I would recommend using a for loop. – Berk Kurkcuoglu Sep 09 '20 at 20:26

1 Answers1

0
IntStream.range(0, originals.size())
        .forEach(i ->  originals.get(i).rawValue = modified.get(i).rawValue);
Shubham
  • 211
  • 2
  • 13