简体   繁体   English

使用Stream API将列表元素复制N次

[英]Copy List elements N times using Stream API

Is there a way to copy some List (or combined string if necessary) N times in Java using Stream API 有没有办法使用Stream API在Java中复制一些List(或必要时组合的字符串)N次

If the list consists of {"Hello", "world"} and N = 3, the result should be {"Hello", "world", "Hello", "world", "Hello", "world"} 如果列表由{"Hello", "world"}和N = 3组成,则结果应为{"Hello", "world", "Hello", "world", "Hello", "world"}

What I've done so far is to get combined String element and I am not sure how to procees to copy it N times. 到目前为止我所做的是获得组合的String元素,我不知道如何进行N次复制。 While I can do it externally, I would like to see if it is possible to do with the help of streams 虽然我可以在外部进行,但我想看看是否可以使用流的帮助

Optional<String> sentence = text.stream().reduce((value, combinedValue) -> { return value + ", " + combinedValue ;});

I would like to use stream, because I am planning to continue with other stream operations after the one above 我想使用流,因为我计划继续上面的其他流操作

You can use Collections.nCopies : 您可以使用Collections.nCopies

List<String> output =
    Collections.nCopies(3,text) // List<List<String>> with 3 copies of 
                                // original List
               .stream() // Stream<List<String>>
               .flatMap(List::stream) // Stream<String>
               .collect(Collectors.toList()); // List<String>

This will product the List : 这将产生List

[Hello, World, Hello, World, Hello, World]

for your sample input. 为您的样本输入。

You can use an IntStream and flatMap to connect the text List multiple times: 您可以使用IntStreamflatMap多次连接text List:

List<String> result = IntStream.range(0, 3)
        .mapToObj(i -> text)
        .flatMap(List::stream)
        .collect(Collectors.toList());

The result looks like this: 结果如下:

[Hello, World, Hello, World, Hello, World]

暂无
暂无

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

相关问题 将一个列表的所有元素/对象 N 次复制到新列表。 使用 java 8 stream - Copy All the elements/object of one list N times to new list. Using java 8 stream 使用 java stream API 检查列表元素是否是整数的连续范围 - Check if list elements are a continuous range of integers using java stream API 使用 Java 8 Stream 的 N 数组元素的总和 - SUM of elements of N Arrays using Java 8 Stream 如何使用 stream 将列表类型 A 和 B 的元素复制到新列表类型 C - How to Copy elements of list type A & B to new list type C using stream 使用流 API 按 Java 中的 n 个字段分组 - Group by n fields in Java using stream API 如何使用Java Streams API进行n次迭代的for循环迭代以实现具有多个动态更改的列表的列表 - How to convert for loop iterating n times to achieve a list using java Streams API with multiple counts changing dynamically 使用Stream API合并两个按N个元素交替排列的列表 - Merging two lists alternating by N elements with Stream API 如何在列表中获取 map 值<integer>到列表中的元素<string>使用 Stream Api 和 Java 8</string></integer> - How to map values in a List<Integer> to elements in a List<String> using Stream Api with Java 8 使用 Stream API 在不使用 distinct() 方法的情况下在整数列表中查找不同元素 - Find Distinct Elements in a List of Integer without using distinct() method using Stream API 使用 Stream API 将两个列表有条件地缩减为包含两个元素的单个列表 - Implement conditional reduction of two lists into a single list containing two elements using Stream API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM