如何在java中将集合列表转换为唯一的数组或列表?

j2cgzkjk  于 2021-06-29  发布在  Java
关注(0)|答案(3)|浏览(249)

我只是在这个方法中寻找每个产品带来的所有注解,但是结果将是一个数组,因此我只是想知道如何以一种简单的方式将其转换为一个唯一的数组。
这是我 swagger 的结果:

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

我们的想法是:

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

在我的一个服务实现中,我设置的方法具有以下逻辑:
服务:

Map<String, Object> getAllComments() throws GeneralException

服务实施:

@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;
}
vngu2lb8

vngu2lb81#

你可以用 flatMap 方法:

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);
}

注:

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

这不是有效的json!{}意思是物体,
您需要的是:

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

这是一个有效的字符串数组。

332nm8kg

332nm8kg2#

我只是修改了一些想法,比如将变量commentsset的类类型注解集列表更改为一个类型类注解列表。然后修改从存储库中带来的每个产品的逻辑,使用flatmap将获得的集合列表展平到一个数组中,然后将其收集到一个列表中。有点:

@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;
}

结果是:

"all_comments": [
  "comment 1 test a ver si sirve",
  "per fecto prueba 3",
  "comment 34 test a ver si sirve test 34"
]
im9ewurl

im9ewurl3#

你可以用 Stream.flatMap 方法将集合列表展平为单个数组,如下所示:

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

另请参见:将二维数组中每列的所有元素相加

相关问题