Java8-Stream: 没有类型变量的实例(s)

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

Java8-Stream: no instance(s) of type variable(s)

问题

以下是翻译好的内容:

  1. public List<String> getPartnersOfClient(String clientId, MyClient[] allMyClients) {
  2. Stream<String> myClient =
  3. Stream.of(allMyClients)
  4. .filter(client -> client.getId() == Integer.valueOf(clientId))
  5. .flatMap(client -> Arrays.stream(client.getPartners())
  6. .map(partner -> partner.getId()))
  7. .collect(Collectors.toList());
  8. return myClient;
  9. }

请注意,翻译中的代码可能需要根据实际情况进行适当的调整。如果有任何问题或需要进一步帮助,请随时提问。

英文:

I have a json as below:

  1. [
  2. {
  3. &quot;id&quot;: 2,
  4. &quot;partners&quot;: [
  5. {
  6. &quot;configuration&quot;: {
  7. &quot;connect-fleet-sync&quot;: false
  8. },
  9. &quot;id&quot;: &quot;cf89cbc5-0886-48cc-81e7-ca6123bc4857&quot;
  10. },
  11. {
  12. &quot;configuration&quot;: {
  13. &quot;connect-fleet-sync&quot;: false
  14. },
  15. &quot;id&quot;: &quot;cf89cbc5-0886-48cc-81e7-ca6123bc4967&quot;
  16. }
  17. ]
  18. },
  19. {
  20. &quot;id&quot;: 3,
  21. &quot;partners&quot;: [
  22. {
  23. &quot;configuration&quot;: {
  24. &quot;connect-fleet-sync&quot;: false
  25. },
  26. &quot;id&quot;: &quot;c3354af5-16e3-4713-b1f5-a5369c0b13d6&quot;
  27. }
  28. ]
  29. }
  30. ]

I need a method which i receive the id and it gives me ids of partners.

Fr example getPartnersOfClient(2) and then i expect it returns:

  1. [&quot;cf89cbc5-0886-48cc-81e7-ca6123bc4857&quot;,&quot;cf89cbc5-0886-48cc-81e7-ca6123bc4857&quot;]

Here is what i have tried:

  1. public List&lt;String&gt; getPartnersOfClient(String clientId, MyClient[] allMyClients) {
  2. Stream&lt;String&gt; myClient =
  3. Stream.of(allMyClients)
  4. .filter(client -&gt; client.getId() == Integer.valueOf(clientId))
  5. .map(MyClient::getPartners)
  6. .map(Partner::getPartnerId)
  7. .collect(Collectors.toList());
  8. return allPartners;
  9. }

But, in the line .map(Partner::getPartnerId) it complains with:

  1. reason: no instance(s) of type variable(s) exist so that Partner[] conforms to Partner

Here is the picture of error:

Java8-Stream: 没有类型变量的实例(s)

答案1

得分: 1

看起来 getPartners() 返回的是 Partner[],而不是一个 Partner,因此你不能在这些数组上使用 Partner::getPartnerId

你可以使用 flatMap 来获得所有单独的 Partner 实例的 Stream<Partner>

  1. Stream<String> fdeClient =
  2. Stream.of(allFdeClients)
  3. .filter(client -> client.getId() == Integer.parseInt(clientId))
  4. .flatMap(f -> Stream.of(f.getPartners()))
  5. .map(Partner::getPartnerId)
  6. .collect(Collectors.toList());
英文:

It looks like getPartners() returns a Partner[], not a Partner, so you can't apply Partner::getPartnerId on these arrays.

You can use flatMap to get a Stream&lt;Partner&gt; of all the individual Partner instances.

  1. Stream&lt;String&gt; fdeClient =
  2. Stream.of(allFdeClients)
  3. .filter(client -&gt; client.getId() == Integer.parseInt(clientId))
  4. .flatMap(f -&gt; Stream.of(f.getPartners()))
  5. .map(Partner::getPartnerId)
  6. .collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年10月15日 17:08:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64368272.html
匿名

发表评论

匿名网友

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

确定