Jeff
Jeff

Reputation: 8411

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

I have a json as below:

[
    {
        "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 i receive the id and it gives me ids of partners.

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

["cf89cbc5-0886-48cc-81e7-ca6123bc4857","cf89cbc5-0886-48cc-81e7-ca6123bc4857"]

Here is what i have tried:

  public List<String> getPartnersOfClient(String clientId, MyClient[] allMyClients) {

    Stream<String> myClient =
        Stream.of(allMyClients)
            .filter(client -> client.getId() == Integer.valueOf(clientId))
            .map(MyClient::getPartners)
            .map(Partner::getPartnerId)
            .collect(Collectors.toList());
    return allPartners;
  }

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

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

Here is the picture of error:

enter image description here

Upvotes: 0

Views: 1671

Answers (1)

Eran
Eran

Reputation: 393771

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<Partner> of all the individual Partner instances.

Stream<String> fdeClient =
    Stream.of(allFdeClients)
        .filter(client -> client.getId() == Integer.parseInt(clientId))
        .flatMap(f -> Stream.of(f.getPartners()))
        .map(Partner::getPartnerId)
        .collect(Collectors.toList());

Upvotes: 1

Related Questions