英文:
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.forEach 是void类型。在它之后无法调用.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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论