简体   繁体   English

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

[英]How to convert a list of sets to a unique array or list in Java?

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.我只是在这种方法中寻找每个产品带来的所有评论,但结果将是一个 arrays 数组,因此只是想知道我怎么能以简单的方式将它变成一个独特的数组。

Here my result on my swagger:这是我在 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;
}

You can use the flatMap method:您可以使用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);
}

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!这不是一个有效的 JSON! {} means an object, {}表示 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.这是一个有效的字符串数组。

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.我只是要修改一些想法,例如将 class 类型注释集列表的变量 commentsSet 更改为 class 类型注释列表。 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.然后修改我从存储库带来的每个产品的逻辑,使用 flatMap 将获得的集合列表展平为一个数组,然后将其收集到一个列表中。 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"
]

You can use Stream.flatMap method to flatten a list of sets into a single array as follows:您可以使用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

See also: Adding up all the elements of each column in a 2d array另请参阅:将二维数组中每列的所有元素相加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM