Java Stream:如何从JSON中查找id?

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

Java Stream: How to find id from json?

问题

我有一个以下的 JSON,以及一个与之类似的类 MyClient

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

我需要一个方法,它接收 UUID,然后返回 id

例如 findClientId("cf89cbc5-0886-48cc-81e7-ca6123bc4967"),然后返回 2

以下是我尝试过的代码:

  1. public static void findClientId(String partnerId, MyClient[] allMyClients) {
  2. Stream.of(allMyClients)
  3. .map(MyClient::getPartners)
  4. .forEach(partners ->
  5. partners.stream()
  6. .filter(partner -> partner.getId().equals(partnerId))
  7. .map(Partner::getClientId)
  8. .forEach(System.out::println)
  9. );
  10. }

但是它报错 Non-static method cannot be referenced from a static context,在 .forEach(Partner::getPartnerId) 这一行。

英文:

I have a json as below and a similar classes respect to it MyClient:

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

I need a method which receives the uuid and it returns id.

Fr example findClientId("cf89cbc5-0886-48cc-81e7-ca6123bc4967") and then it returns 2:

Here is what i have tried:

  1. public static void findClientId(String partnerId, MyClient[] allMyClients) {
  2. Stream.of(allMyClients)
  3. .map(MyClient::getPartners)
  4. .forEach(Partner::getPartnerId)
  5. .filter(
  6. partner ->partner.id.equals(partnerId)))
  7. .map(MyClient::getId);
  8. }

But it complains Non-static method cannot be referenced from a static context
in the line .forEach(Partner::getPartnerId)

答案1

得分: 2

首先,Stream.forEachvoid类型。在它之后无法调用.filter或任何其他方法。除此之外,.map(MyClient::getPartners) 会使你在流水线中丢失 MyClient 的信息,而你实际上想要的是 MyClient.getId。你需要在每个客户端的合作伙伴列表上使用嵌套流进行过滤。

你的代码应该是:

  1. public static String findClientId(String partnerId, MyClient[] allMyClients) {
  2. return Stream.of(allMyClients)
  3. .filter(client -> client.getPartners()
  4. .stream()
  5. .anyMatch(partner -> partner.getPartnerId().equals(partnerId)))
  6. .map(MyClient::getId)
  7. .findAny()
  8. .orElse(null); // 根据需要更改
  9. }
英文:

First, Stream.forEach is void. You can't invoke .filter or any other method after it. Beside that, .map(MyClient::getPartners) would make you lose MyClient information in the pipeline while it's MyClient.getId that you want. You'd have to use a nested stream to for filtering on partner list in each client.

Your code should be:

  1. public static String findClientId(String partnerId, MyClient[] allMyClients) {
  2. return Stream.of(allMyClients)
  3. .filter(client -> client.getPartners()
  4. .stream()
  5. .anyMatch(partner -> partner.getPartnerId().equals(partnerId)))
  6. .map(MyClient::getId)
  7. .findAny()
  8. .orElse(null); // change to whatever is desired
  9. }

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

发表评论

匿名网友

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

确定