I'm iterating list of string and checking if string contains colon :
, if it does then split it and assign result in variables. Following code works but how to write it effectively using Java 8 lambda expression.
String key = null;
String value = null;
List<String> testList = new ArrayList<>();
testList.add("value1");
testList.add("value2");
testList.add("value3");
testList.add("value4");
testList.add("value5:end");
for(String str : testList) {
if(str.contains(":" )) {
String[] pair = str.split(":");
key = pair[0];
value = pair[1];
}
}
Tried the equivalent of the above code in Java 8, however I'm getting an error that Local variable in an enclosing scope must be final or effectively final.
testList.stream().forEach(str -> {
if(str.contains(":" )) {
String[] pair = str.split(":");
key = pair[0];
value = pair[1];
}
});