dimanche 5 mai 2019

Is there a way to simulate property selectors in java?

In C# we have the option to specify a property of an object using property selectors (for example when using LINQ) like so:

var usernames = customers.Where(x => x.Age > 20).Select(x => x.Username);

Here we first select the property Age to perform the comparison on and then specify the property Username to the Select(...) clause.

I am currently trying to replicate this functionality using lambda expressions in Java to enable users of my code to specify which property should be used for some action later on. The final result should look similar to the following:

public class Builder<T> {

// ...
private Field field;

Builder<T> forField(SomeFunctionalInterface s) {
    this.field = s.evaluate();
    return this;
 }
// ...
}

Thank you for your effort.





Aucun commentaire:

Enregistrer un commentaire