1

I'm just in this method where I'm looking for all comments that each product brings, but the result would be an array of arrays, thus just wondering how could I, in a simple way turn this into a unique array.

Here my result on my swagger:

"all_comments": [
  [
    "comment 1 test a ver si sirve",
    "comment 34 test a ver si sirve test 34"
  ],
  ["comment 2xxxxxxxxx",
    "comment yyyyyyyy"
  ]
]

And the idea is to have kind of:

"all_comments": [
  "comment 1 test a ver si sirve",
  "comment 34 test a ver si sirve test 34",
  "comment 2xxxxxxxxx",
  "comment yyyyyyyy"
]

On one of my service implementation the method I set has this logic:

Service:

Map<String, Object> getAllComments() throws GeneralException

Service Implementation:

@Override
public Map<String, Object> getAllComments() throws GeneralException {
    Map<String, Object> comments = new HashMap<>();
    List<Set<Comments>> commentsSet = productRepository.findAll().stream()
            .map(product -> product.getCommentsSet())
            .collect(Collectors.toList());
    comments.put("all_comments", commentsSet.stream()
            .map(passList -> passList.stream()
                    .map(passSet -> passSet.getCommentBody()))
            .collect(Collectors.toList()));
    return comments;
}
Enrique GF
  • 1,215
  • 3
  • 16
  • 35
  • Does this answer your question? [How do I take the union of sets?](https://stackoverflow.com/questions/65922558/how-do-i-take-the-union-of-sets) –  Jun 13 '21 at 22:44

3 Answers3

0

You can use the flatMap method:

public static void main(String[] args) {
    final List<List<String>> comments = List.of(
            List.of("Matheus", "Rambo"),
            List.of("Stack Overflow", "Github"),
            List.of("What am I doing?", "This is a comment!"),
            List.of("Github", "Rambo"));

    final Set<String> allComments = comments.stream()
            .flatMap(List::stream)
            .collect(Collectors.toSet());

    System.out.println(allComments);
}

Note:

"all_comments": [
  {
    "comment 1 test a ver si sirve",
    "comment 34 test a ver si sirve test 34",
    "comment 2xxxxxxxxx",
    "comment yyyyyyyy"
  }
]

This is not a valid JSON! {} means an object,

what you need is:

"all_comments": [
    "comment 1 test a ver si sirve",
    "comment 34 test a ver si sirve test 34",
    "comment 2xxxxxxxxx",
    "comment yyyyyyyy"
]

This one is a valid array of strings.

Community
  • 1
  • 1
sgtcortez
  • 407
  • 4
  • 15
0

I just got to modify some ideas, like changing the variable commentsSet of List of Sets of class type Comments to just a list of type class Comments. Then modifying the logic of my each product brought from repository, using the flatMap that list of sets obtained is flattened to one array, collecting it to a list after that. kind of:

@Override
public Map<String, Object> getAllComments() throws GeneralException {
    Map<String, Object> comments = new HashMap<>();
    List<Comments> commentsSet = productRepository.findAll().stream()
            .map(product -> product.getCommentsSet())
            .flatMap(x -> x.stream())
            .collect(Collectors.toList());
    comments.put("all_comments", commentsSet.stream()
            .map(passList -> passList.getCommentBody())
            .collect(Collectors.toList()));
    return comments;
}

Having as result :

"all_comments": [
  "comment 1 test a ver si sirve",
  "per fecto prueba 3",
  "comment 34 test a ver si sirve test 34"
]
Community
  • 1
  • 1
Enrique GF
  • 1,215
  • 3
  • 16
  • 35
0

You can use Stream.flatMap method to flatten a list of sets into a single array as follows:

List<Set<String>> commentsSet = List.of(
        new LinkedHashSet<>(List.of(
                "comment 1 test a ver si sirve",
                "comment 34 test a ver si sirve test 34")),
        new LinkedHashSet<>(List.of(
                "comment 2xxxxxxxxx",
                "comment yyyyyyyy")));

String[] commentsArr = commentsSet.stream()
        .flatMap(Collection::stream)
        .toArray(String[]::new);

Arrays.stream(commentsArr).forEach(System.out::println);
// comment 1 test a ver si sirve
// comment 34 test a ver si sirve test 34
// comment 2xxxxxxxxx
// comment yyyyyyyy

See also: Adding up all the elements of each column in a 2d array