I have a problem with lambda expressions in java.
List<String> l = new ArrayList<String>();
l.add("Besit");
l.add("Java");
String str = "Java";
boolean flag = false;
int counter = 0;
l.forEach((h) -> {
if (h.equals(str)) {
counter++;
flag = true.
}
});
Obviously, I have error, because forEach doesn't see str and flag, I know that in C++ lambdas we can pass some parameters by reference. Is it possible to do this in java?
h = "Change"does not change the value in the list. It just changes what local variablehrefers to. If you want to change the value in the list, you must use aListIterator, so you can't use Streams for that. If you must use Streams, usemap(...)and build a new list.set().set()" Would be better to use aListIteratorand itsset()method, since that'll perform much better if the list is e.g. aLinkedList.