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

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

Java Stream: How to find id from json?

问题

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

[
    {
        "id": 2,
        "partners": [
            {
                "configuration": {
                    "connect-fleet-sync": false
                },
                "id": "cf89cbc5-0886-48cc-81e7-ca6123bc4857"
            },
            {
                "configuration": {
                    "connect-fleet-sync": false
                },
                "id": "cf89cbc5-0886-48cc-81e7-ca6123bc4967"
            }
        ]
    },
    {
        "id": 3,
        "partners": [
            {
                "configuration": {
                    "connect-fleet-sync": false
                },
                "id": "c3354af5-16e3-4713-b1f5-a5369c0b13d6"
            }
        ]
    }
]

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

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

以下是我尝试过的代码:

public static void findClientId(String partnerId, MyClient[] allMyClients) {
    Stream.of(allMyClients)
        .map(MyClient::getPartners)
        .forEach(partners ->
            partners.stream()
                .filter(partner -> partner.getId().equals(partnerId))
                .map(Partner::getClientId)
                .forEach(System.out::println)
        );
}

但是它报错 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:

[
    {
        "id": 2,
        "partners": [
            {
                "configuration": {
                    "connect-fleet-sync": false
                },
                "id": "cf89cbc5-0886-48cc-81e7-ca6123bc4857"
            },
            {
                "configuration": {
                    "connect-fleet-sync": false
                },
                "id": "cf89cbc5-0886-48cc-81e7-ca6123bc4967"
            }
        ]
    },
    {
        "id": 3,
        "partners": [
            {
                "configuration": {
                    "connect-fleet-sync": false
                },
                "id": "c3354af5-16e3-4713-b1f5-a5369c0b13d6"
            }
        ]
    }
]

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:

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

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。你需要在每个客户端的合作伙伴列表上使用嵌套流进行过滤。

你的代码应该是:

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

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:

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

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:

确定