dimanche 18 février 2018

How to dynamically create a generic lambda implementation by type?

For example, i have class

public class Human {

  private String name;

  ...
}

and i want to implement something like this:

(1)

List<Human> humans =  initHumans();
Equals<Human> humanEquals = new Equals<>
Predicate<Human> filter = humanEquals.filter("name", "John");
List<Human> filteredHumans = humans
    .stream()
    .filter(filter)
    .collect(Collectors.toList());

Equals:

public class Equals<T> extends AbstractPredicate<T> {

  public java.util.function.Predicate<T> filter(String fieldName, String 
  fieldValue) {
    ....
  }  
}

is it posiible implement filter method to provide the (1) behavior?

I want to return Predicate like this:

Predicate<Human> predicate= human -> human.getName().equals("John");

similarly should work for other classes:

Predicate<Car> filter = humanEquals.filter("color", "red");
//like this:
Predicate<Car> predicate= human -> human.getColor().equals("red");





Aucun commentaire:

Enregistrer un commentaire