【发布时间】:2018-09-17 18:23:33
【问题描述】:
我有列表列表。我需要根据索引从这些列表中提取项目并使其成为单独的数组列表。我尝试通过添加来做到这一点
List<List<String>> multilist = new ArrayList<>();
List<List<String>> totalRecords= totalRecordsList;
List<String> targetList = totalRecords.stream().filter(e ->
e.get(index)!=null).flatMap(List::stream) .collect(Collectors.toCollection(ArrayList::new));
multilist.add(targetList);
但仍然在列表列表中,而不是作为单独的 arraylist 对象存储,它正在组合所有项目。你能纠正我错的地方吗?
谢谢
【问题讨论】:
-
List<List<String> = totalRecords;这在语法上不准确 -
请提供minimal reproducible example。您当前的代码 sn-p 有许多语法错误,使您难以理解您要执行的操作。确保在您的代码周围包含一个类和方法,以便我们可以复制并粘贴它来自己编译。
-
应该是 List
- totalRecords = totalRecordsList;
-
flatMap就是这样做的,它将列表列表扁平化为一维列表。删除该方法调用。 -
你的意思是
map(e -> e.get(index))而不是flatMap?
标签: java arraylist java-8 java-stream