在Java中按照responseEntity在列表中进行筛选。

huangapple go评论69阅读模式
英文:

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&lt;Gruoup&gt; listaGrupo = monstarListaGrupo(login, token);
List&lt;Diretorio&gt; diretorios = lista.stream()
        .filter(x-&gt; 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:

    {
        &quot;name&quot;: &quot;barry&quot;,
        &quot;age&quot;: 20
    },
	        {
        &quot;name&quot;: &quot;allan&quot;,
        &quot;age&quot;: 17
    },
	 {
        &quot;name&quot;: &quot;julie&quot;,
        &quot;age&quot;: 20
    },
	 {
        &quot;name&quot;: &quot;jord&quot;,
        &quot;age&quot;: 19
    }

List:

    { 
        &quot;name&quot;: &quot;jhenny&quot;,
        &quot;age&quot;: 20,
		&quot;color&quot;: &quot;red&quot;
		
    },
	{
        &quot;name&quot;: &quot;barry&quot;,
        &quot;age&quot;: 20,
		&quot;color&quot;: &quot;black&quot;
    },
	 {
        &quot;name&quot;: &quot;julie&quot;,
        &quot;age&quot;: 20,
		&quot;color&quot;: &quot;white&quot;
    }

Result final List

	{
        &quot;name&quot;: &quot;barry&quot;,
        &quot;age&quot;: 20,
		&quot;color&quot;: &quot;black&quot;
    },
	 {
        &quot;name&quot;: &quot;julie&quot;,
        &quot;age&quot;: 20,
		&quot;color&quot;: &quot;white&quot;
    }

答案1

得分: 1

您正在尝试将一个 StringList&lt;Grupo&gt; 进行比较,但似乎您想要基于响应实体中包含的 grupoAd 来进行过滤。

首先,将列表中的字符串收集到一个集合中,然后使用 contains() 进行过滤,类似这样:

ResponseEntity&lt;Gruoup&gt; listaGrupo = monstarListaGrupo(login, token);
Set&lt;String&gt; names = listaGrupo.getBody().getGrupos().stream().collect(toSet());
List&lt;Diretorio&gt; diretorios = lista.stream()
    .filter(x -&gt; names.contains(x.getGrupoAd()))
    ...
英文:

You are trying to compare a String with a List&lt;Grupo&gt;, 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&lt;Gruoup&gt; listaGrupo = monstarListaGrupo(login, token);
Set&lt;String&gt; names = listaGrupo.getBody().getGrupos().stream().collect(toSet());
List&lt;Diretorio&gt; diretorios = lista.stream()
    .filter(x -&gt; names.contains(x.getGrupoAd()))
    ...

huangapple
  • 本文由 发表于 2020年9月22日 06:20:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64000675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定