将list< list< string>>转换为list< string>java

nr9pn0ug  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(780)

我已经 ArrayList<List<String>> 其中包含数据。例如:

  1. [[1,2,3],[1,2,3],[1,2,3]]

我试着把它转换成 List<String> 但它变成了

  1. [1,2,3,1,2,3,1,2,3]

代码:

  1. List<String> f =
  2. ch.stream()
  3. .flatMap(List::stream)
  4. .collect(Collectors.toList());
  5. System.out.println(f);

如何将其转换为以下输出,以便通过 list.get(index) ```
List list = [1,2,3],[1,2,3],[1,2,3];

mtb9vblg

mtb9vblg1#

你在追求下列目标吗?

  1. List<List<String>> ch = List.of(List.of("1", "2", "3"), List.of("1", "2", "3"), List.of("1", "2", "3"));
  2. System.out.println(ch);
  3. List<String> f = ch.stream()
  4. .map(List::toString)
  5. .collect(Collectors.toList());
  6. System.out.println(f);

在输出中,两个列表看起来相同:

  1. [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
  2. [[1, 2, 3], [1, 2, 3], [1, 2, 3]]

但是我们可以从代码中看到,前者是一个列表列表,后者只是一个字符串列表。

展开查看全部

相关问题