Filter in list by responseEentity in Java

182 Views Asked by At

I need to filter a list with a response

   ResponseEntity<Gruoup> listaGrupo = monstarListaGrupo(login, token);
List<Diretorio> diretorios = lista.stream()
        .filter(x-> x.getGrupoAd() == listaGrupo.getBody().getGrupos())

I tried to do it that way but he doesn't let it because it says that one type is list and another is String

Apartment filter of a RespondeEntity in an age list for exam

RespondeEntity:

    {
        "name": "barry",
        "age": 20
    },
            {
        "name": "allan",
        "age": 17
    },
     {
        "name": "julie",
        "age": 20
    },
     {
        "name": "jord",
        "age": 19
    }

List:

    { 
        "name": "jhenny",
        "age": 20,
        "color": "red"
        
    },
    {
        "name": "barry",
        "age": 20,
        "color": "black"
    },
     {
        "name": "julie",
        "age": 20,
        "color": "white"
    }

    

Result final List

    {
        "name": "barry",
        "age": 20,
        "color": "black"
    },
     {
        "name": "julie",
        "age": 20,
        "color": "white"
    }
    
1

There are 1 best solutions below

1
On

You are trying to compare a String with a List<Grupo>, but it seems you want to filter based on the response entity containing the grupoAd of the directio.

First collect the Strings from the List into a Set, then use contains() for the filter, somethingh like this:

ResponseEntity<Gruoup> listaGrupo = monstarListaGrupo(login, token);
Set<String> names = listaGrupo.getBody().getGrupos().stream().collect(toSet());
List<Diretorio> diretorios = lista.stream()
    .filter(x -> names.contains(x.getGrupoAd()))
    ...