In Java 8, with the following class
class Person {
private boolean born;
Person() {
}
public void setBornTrue() {
born = true;
}
public void setBorn(boolean state) {
born = state;
}
}
it is possible to call the setBornTrue method via a method reference:
ArrayList<Person> people = new ArrayList<>();
people.add(new Person());
people.forEach(Person::setBornTrue);
but how would I use the forEach method and use the setBorn using a method reference? Trying:
people.forEach(Person::setBorn);
results in an error, "Cannot resolve method setBorn".
In addition, how would I pass in the value of True?
With lambda:
Found no other ways only using the java 8 API.
With this custom function:
You can do:
If this kind of utility methods is available in the java API or in a library, please let us know.