Java8-如何添加列表< lt; string>>列表< lt; string>>

发布于 2025-02-03 05:12:23 字数 841 浏览 2 评论 0 原文

我有两个 list< list< string> ,并希望将它们串联。

这是我的代码:

List<List<String>> a = Arrays.asList(Arrays.asList("a", "b", "c"));
List<List<String>> b = Arrays.asList(Arrays.asList("d", "e", "f"));
a.addAll(b);

但是,它不起作用并引发异常。

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:148)
    at java.util.AbstractList.add(AbstractList.java:108)
    at java.util.AbstractCollection.addAll(AbstractCollection.java:344)

下面的代码有效:

List<List<String>> a = new ArrayList<>();
a.add(Arrays.asList("a", "b", "c"));
List<List<String>> b = new ArrayList<>();
b.add(Arrays.asList("d", "e", "f"));
a.addAll(b);

这里有什么区别?

I have two List<List<String>> and would like to concatenate them.

Here is my code:

List<List<String>> a = Arrays.asList(Arrays.asList("a", "b", "c"));
List<List<String>> b = Arrays.asList(Arrays.asList("d", "e", "f"));
a.addAll(b);

However, it doesn't work and throws an exception.

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:148)
    at java.util.AbstractList.add(AbstractList.java:108)
    at java.util.AbstractCollection.addAll(AbstractCollection.java:344)

The code below works:

List<List<String>> a = new ArrayList<>();
a.add(Arrays.asList("a", "b", "c"));
List<List<String>> b = new ArrayList<>();
b.add(Arrays.asList("d", "e", "f"));
a.addAll(b);

What's the difference here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

思念绕指尖 2025-02-10 05:12:23

您尝试使用 a.addall(b)只要 exter List a 是可变的,并且您不介意修改它。

可变的外部列表:

List<List<String>> a = new ArrayList<>(List.of(
        List.of("a", "b", "c"),
        List.of("d", "e", "f")));

List<List<String>> b = new ArrayList<>(List.of(
        List.of("h", "i", "j"),
        List.of("k", "l", "m")));

a.addAll(b); // the 'a' will contain all the inner lists

a.forEach(System.out::println);

不变的外部列表

如果要保留前 a b 列表和/或产生一个新的,使用 java-stream

List<List<String>> a = List.of(
        List.of("a", "b", "c"),
        List.of("d", "e", "f"));

List<List<String>> b = List.of(
        List.of("h", "i", "j"),
        List.of("k", "l", "m"));

List<List<String>> result = Stream.of(a, b)
        .flatMap(Collection::stream)
        .collect(Collectors.toList());

result.forEach(System.out::println);

结果

两种方式共享相同的输出:

  [a,b,c]
[D,E,F]
[H,I,J]
[K,L,M]
 

内部列表是否不可变,对于这种组合而言并不重要。

Your attempt with a.addAll(b) works as long as the outer list a is mutable and you don't mind to modify it.

Mutable outer list:

List<List<String>> a = new ArrayList<>(List.of(
        List.of("a", "b", "c"),
        List.of("d", "e", "f")));

List<List<String>> b = new ArrayList<>(List.of(
        List.of("h", "i", "j"),
        List.of("k", "l", "m")));

a.addAll(b); // the 'a' will contain all the inner lists

a.forEach(System.out::println);

Immutable outer list

In case you want to preserve the former a and b lists and/or yield a new one, use :

List<List<String>> a = List.of(
        List.of("a", "b", "c"),
        List.of("d", "e", "f"));

List<List<String>> b = List.of(
        List.of("h", "i", "j"),
        List.of("k", "l", "m"));

List<List<String>> result = Stream.of(a, b)
        .flatMap(Collection::stream)
        .collect(Collectors.toList());

result.forEach(System.out::println);

Result

Both ways share the same output:

[a, b, c]
[d, e, f]
[h, i, j]
[k, l, m]

Whether the inner lists are immutable or not doesn't matter for such combining.

我三岁 2025-02-10 05:12:23

您的代码不起作用,因为 arrays.aslist()返回

由指定数组支持的固定尺寸列表

https://docs.oracle.com/en/java/javase/17/docs/api/java.base.base.base/java/java/java/util/arrays.html.html#aslist( t ...)

这意味着,当您尝试添加更多元素时,数组中没有代表 list 的空间;因此抛出 unsupportedOperationException

如果要使用特定元素构建 list ,并且还可以扩展其大小,则应通过提供 aslist() aslist()<的列表来使用类'转换构造函数/代码>方法。

List<List<String>> a = new ArrayList<>(Arrays.asList(new ArrayList<>(Arrays.asList("a", "b", "c"))));
List<List<String>> b = new ArrayList<>(Arrays.asList(new ArrayList<>(Arrays.asList("d", "e", "f"))));
a.addAll(b);

这样,您的外部和内部列表都将以实际的 list 行为,而无需在超过原始大小的情况下扔 UnsupportedOperationException

Your code doesn't work because the Arrays.asList() returns

a fixed-size list backed by the specified array

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Arrays.html#asList(T...)

This means that when you try to add further elements, there is no space within the array representing the List; thus throwing anUnsupportedOperationException.

If you want to build List with specific elements and also be able to extend its size, then you should employ the class' Conversion Constructor by providing to it the lists returned by the asList() method.

List<List<String>> a = new ArrayList<>(Arrays.asList(new ArrayList<>(Arrays.asList("a", "b", "c"))));
List<List<String>> b = new ArrayList<>(Arrays.asList(new ArrayList<>(Arrays.asList("d", "e", "f"))));
a.addAll(b);

This way, both your outer and inner lists will behave as an actual List without throwing an UnsupportedOperationException when exceeding the original size.

情释 2025-02-10 05:12:23

您可以避免使用addall函数并通过B进行迭代,并添加到如下所示的类似:

for(innerList:b){
   a.add(innerList);
}

You could avoid using the addAll function and iterate through b and add to a like shown below:

for(innerList:b){
   a.add(innerList);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文