public void test(){ String x; List<String> list=Arrays.asList("a","b","c","d"); list.forEach(n->{ if(n.equals("d")) x="match the value"; }); }
1.Like the code above, I want to set the value of a variable beside the foreach block, can it works?
2.And why?
3.And the foreach iterator is in order or disorder?
4.I think the lamdas foreach block is cool and simple for iterator,but this is really a complicated thing to do rather than the same work in java 7 or before.
You could, of course, "make the outer value mutable" via a trick:
public void test() { String[] x = new String[1]; List<String> list = Arrays.asList("a", "b", "c", "d"); list.forEach(n -> { if (n.equals("d")) x[0] = "match the value"; }); }
Get ready for a beating by the functional purist on the team, though. Much nicer, however, is to use a more functional approach (similar to Sleiman's approach):
public void test() { List<String> list = Arrays.asList("a", "b", "c", "d"); String x = list.stream() .filter("d"::equals) .findAny() .map(v -> "match the value") .orElse(null); }
effectively final
. you can achieve the same more concisely using filter
and map
.
Optional<String> d = list.stream() .filter(c -> c.equals("d")) .findFirst() .map(c -> "match the value");
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