I have a list like below
List<String> fruits = new ArrayList<>();
fruits.add("apple");
fruits.add("mango");
fruits.add("grapes");
System.out.println(fruits.toString());
I am using lambda expression for printing the list like
fruits.forEach(item->System.out.println(item));
and its working fine my requirement is I need to iterate over the list and concatenate the items to a string
String stringFruits = "";
fruits.forEach(item->stringFruits = stringFruits+item);
this is giving a compile time error saying variable values used in lambda should be effectively final is there any way I can do it in java 8 ?
String stringFruits = fruits.stream().collect(Collectors.joining());