Function . filter() for a List<Class>

Asked

Viewed 270 times

3

I have a class Pessoa that has the columns Integer id. String name, and String date_born.

In my main Activity I am already retrieving the list of people that comes from my request made in the API and already send to my function to group.

private void loadList(List<Pessoas> listPessoas){
       Map<String, List<Pessoas>> listMap = listPessoas.stream().collect(Collectors.groupingBy(Pessoa::getDate_born)); //LISTA AGRUPADA
}

But I need to filter the list before I can group through the field date_born, There goes my question.

private void loadList(List<Pessoas> listPessoas){
      List<Pessoas> listPessoasFiltered = ??           
      Map<String, List<Pessoas>> listMap = listPessoasFiltered.stream().collect(Collectors.groupingBy(Pessoa::getDate_born));
}

There is already a type function .filter() that returns the list or has to implement a filter function to return my filtered people list before grouping people by this field?

Remembering is not within an Adapter is a list filter.

1 answer

4


You don’t need to create another filtered list, you can enjoy using it streams and use the method filter:

Map<String, List<Pessoa>> map = listPessoas.stream()
    // filtrar pelo critério que vc precisa
    .filter(pessoa -> pessoa.getName().endsWith("o"))
    // agrupar por data de nascimento
    .collect(Collectors.groupingBy(Pessoa::getDate_born));

In the above example, I am filtering the names that end in "o", but you can use any criteria you wish (the important thing is that the lambda past to filter return true for the elements you want to keep).

  • 1

    I’m crying with happiness, I thought I needed to create a function for it , now that I saw that there is a ready one just use vlw.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.