I am building an object with a simple loop:
WebTarget target = getClient().target(u);
for (Entry<String, String> queryParam : queryParams.entrySet()) {
    target = target.queryParam(queryParam.getKey(), queryParam.getValue());
}
I want to do the same thing using the Java8 Stream API but I cannot figure out how to do it. What makes me struggle is that target is reassigned every time, so a simple .forEach() will not work. I guess I need to use a .collect() or reduce() since I am looking for a single return value but I am lost at the moment!
                        
There's unfortunately no
foldLeftmethod in the stream API. The reason for this is explained by Stuart Marks in this answer:Ultimately what you're trying to do here is something procedural / sequential so I don't think the stream API is a good fit for this use case. I think the for-each loop that you have posted yourself is as good as it gets.
Update:
As @TagirValeev points out below you can in fact solve it with the stream API (using
forEachOrdered. Your code would then look something likeI stand by my original answer though, and claim that your good old for-loop is a better approach in this case.