'Check if each map of the list contains all key-values combination from two sources via Stream api

I have a small service which takes two arrays person and document and then return some array with combined data from them If request like this:

{
  "documents": [
    {
      "id": "A",
    },
    {
      "id": "B",
    }
  ],
  "persons": [
    {
      "lastName": "C",
    },
    {
       "lastName": "D",
    }
  ]
}

Then the response has an array like this:

{
  "documents": [
    {
      "id": "A",
      "lastName": "C"
    },
    {
      "id": "A",
      "lastName": "D"
    },
     {
      "id": "B",
      "lastName": "C"
    }, 
    {
      "id": "B",
      "lastName": "D"
    }
  ]
}

If i get List<Map<String,String>> of resulting array how do i check if each map has each Key-Value combinations via Stream Api. I managed to do this with brute loops but struggling with Stream Api

Update: my method eventually looks like this

 private void checkResultList(List<Map<String, String>> resultList) {

    List<String> persons = Arrays.asList("C", "D");
    List<String> documents = Arrays.asList("A", "B");

    List<Map<String, String>> expectedList = new ArrayList<>();
    List<Map<String, String>> actualList = new ArrayList<>();

     for (String name : persons){
        for (String id : documents){
            Map<String,String> expectedElement = new HashMap<>();
            expectedElement.put("lastName", name);
            expectedElement.put("id", id);
            expectedList.add(expectedElement);
        }
    }
    
       resultList.stream().forEach(i -> {
        Map<String,String> actualElement = new HashMap<>();
        actualElement.put("lastName", i.get("lastName"));
        actualElement.put("id", i.get("id"));
        actualList.add(actualElement);
    });
    
    
    Assertions.assertEquals(actualList, expectedList);
}

Update 2 So far i managed to implement it like this. But i still have two steps. And can't find out if it can be written any shorter.

private void checkResultList(List<Map<String, String>> resultList) {

        List<String> persons = Arrays.asList("C", "D");
        List<String> documents = Arrays.asList("A", "B");

        List<Map<String, String>> expectedList = new ArrayList<>();
        List<Map<String, String>> actualList = new ArrayList<>();

         persons.forEach(i -> documents.forEach(k -> {
            HashMap<String, String> e = new HashMap<>();
            e.put("id", k);
            e.put("name", i);
            expectedList.add(e);
        }));
        
           resultList.stream().forEach(i -> {
        Map<String,String> actualElement = new HashMap<>();
        actualElement.put("lastName", i.get("lastName"));
        actualElement.put("id", i.get("id"));
        actualList.add(actualElement);
    });
    
    Assertions.assertEquals(actualList, expectedList);
        
    }


Solution 1:[1]

I just learned this.. How to flat map of values to another map grouped by key

 List<Map<String, String>> expectedList = 
 documents
    .stream()
    .flatMap(d -> persons.stream().map(p -> Map.of("id", d, "lastName", p)))
    .collect(toList());

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Krishna