java - How better can you rewrite these lambdas? -


consider following class

class person {  private string firstname;  private string lastname;  private int age;  private address address;  //assume getters available  } 

lambda #1 : filter persons of specific age

public list<person> getpersonsofage(int age) {     list<person> persons= fillpersonsinfo();     persons.stream().fiter(person -> person.getage() == 21).collect(collectors.tolist()); } 

lambda #2 : filter persons specific last name

public list<person> getpersonsaboveage(string name) {     list<person> persons= fillpersonsinfo();     persons.stream().fiter(person -> person.getlastname().equals(name)).collect(collectors.tolist()); } 

lambda #2 : filter persons living @ given address

public list<person> getpersonsaboveage(address address) {     list<person> persons= fillpersonsinfo();     persons.stream().filter(person -> person.getaddress().equals(address)).collect(collectors.tolist()); } 

how compact can make these lambdas? can reduce 3 lambdas single function reference?

you can have helper method other methods call, , pass in filter each type of method

 private list<person> filterpeople(predicate<person> filter){      list<person> persons= fillpersonsinfo();      return persons.stream()            .filter(filter)            .collect(tolist());  }   public list<person> getpeopleofage(int age){      return filterpeople(person -> person.getage == age);  }   public list<person> getpeopleofname(string name){      return filterpeople(person -> person.getname().equals(name));  } 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -