英文:
Filter in list by responseEentity in Java
问题
我需要使用响应来过滤一个列表
ResponseEntity<Group> listaGrupo = monstarListaGrupo(login, token);
List<Directory> diretorios = lista.stream()
        .filter(x -> x.getGroupAd().equals(listaGrupo.getBody().getGroups()))
我尝试以这种方式去做,但是它不允许,因为它说一个类型是列表,另一个类型是字符串。
使用响应实体对年龄列表进行公寓过滤
响应实体:
{
    "name": "barry",
    "age": 20
},
{
    "name": "allan",
    "age": 17
},
{
    "name": "julie",
    "age": 20
},
{
    "name": "jord",
    "age": 19
}
列表:
{
    "name": "jhenny",
    "age": 20,
    "color": "red"
},
{
    "name": "barry",
    "age": 20,
    "color": "black"
},
{
    "name": "julie",
    "age": 20,
    "color": "white"
}
最终结果列表:
{
    "name": "barry",
    "age": 20,
    "color": "black"
},
{
    "name": "julie",
    "age": 20,
    "color": "white"
}
英文:
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
得分: 1
您正在尝试将一个 String 与 List<Grupo> 进行比较,但似乎您想要基于响应实体中包含的 grupoAd 来进行过滤。
首先,将列表中的字符串收集到一个集合中,然后使用 contains() 进行过滤,类似这样:
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()))
    ...
英文:
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()))
    ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论