Obj1 x = null;
String key = null;
while(it.hasNext()){
x = it.next();
key = x.getKey();
listObject.stream().filter(e -> e.getKey().equals(key)).findFirst().get();
}
This is complaining with the following message: "Local variable key must be final or effective final..." I understand the the lambda context is created at runtime and it needs variables that do not change.
How can I use the functional style to find the object in the list?
I want to avoid the old fashion way of iterating over the list with while
.
You can simply move the variable declarations to within the loop, thus making them effectively final:
...
while(it.hasNext()){
Obj1 x = it.next();
String key = x.getKey();
listObject.stream().filter(e -> e.getKey().equals(key)).findFirst().get();
}
JIT/etc makes declaring variables outside of a loop a non-issue, really.
In this case you would just need to make key final. For instance, this should work:
while(it.hasNext()){
Obj1 x = it.next();
listObject.stream()
.filter(e -> e.getKey().equals(x.getKey()))
.findFirst().get();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With