.collect(Collectors.toList()
时间: 2024-06-01 07:09:39 浏览: 71
This is a method used in Java to collect the elements of a stream and return them as a list.
For example, if we have a stream of integers and we want to collect them into a list, we can use the following code:
List<Integer> integerList = integerStream.collect(Collectors.toList());
This will create a new list of integers containing all the elements in the stream. We can then use this list for further processing or output.
相关问题
List<RsysMenu> rootMenus = list.stream().filter(item -> "M".equals(item.getParentId())).sorted().collect(Collectors.toList()); List<RsysMenu> rootMenus = list.stream().filter(item -> "M".equals(item.getParentId())).sorted().collect(Collectors.toList()); List<RsysMenu> rootMenus = list.stream().filter(item -> "M".equals(item.getParentId())).sorted().collect(Collectors.toList()); List<RsysMenu> rootMenus = list.stream().filter(item -> "M".equals(item.getParentId())).sorted().collect(Collectors.toList()); List<RsysMenu> rootMenus = list.stream().filter(item -> "M".equals(item.getParentId())).sorted().collect(Collectors.toList());
这段代码是一个Java代码片段,它的作用是从一个列表中过滤出所有父节点为"M"的菜单项,并按照一定规则进行排序,最后将结果收集到一个新的列表中。
首先,通过使用`stream()`方法将列表转换为流,然后使用`filter()`方法过滤出父节点为"M"的菜单项。接着,使用`sorted()`方法对菜单项进行排序。最后,使用`collect()`方法将过滤和排序后的菜单项收集到一个新的列表中。
请注意,代码中多次出现的重复行是输入错误,应该删除多余的行。正确的代码应该是这样的:
```java
List<RsysMenu> rootMenus = list.stream()
.filter(item -> "M".equals(item.getParentId()))
.sorted()
.collect(Collectors.toList());
```
这样,你就可以得到一个包含所有父节点为"M"的菜单项,并按照排序规则排列的列表。
优化以下代码: FileMatrixVo fileMatrixVo = new FileMatrixVo(); fileMatrixVo.setId(tableName + "-" + columnName); fileMatrixVo.setCoherenceFiles(errorOutputFiles.stream().filter(errorOutputFileVo -> RuleTemplateName.ENUMERATION_CHECK.getId().equals(errorOutputFileVo.getRuleTemplateId())).collect(Collectors.toList())); fileMatrixVo.setEffectiveFiles(errorOutputFiles.stream().filter(errorOutputFileVo -> RuleTemplateName.REGEXP_CHECK.getId().equals(errorOutputFileVo.getRuleTemplateId())).collect(Collectors.toList())); fileMatrixVo.setCompleteFiles(errorOutputFiles.stream().filter(errorOutputFileVo -> RuleTemplateName.NULL_CHECK.getId().equals(errorOutputFileVo.getRuleTemplateId())).collect(Collectors.toList())); fileMatrixVo.setUniquenessFiles(errorOutputFiles.stream().filter(errorOutputFileVo -> RuleTemplateName.UNIQUENESS_CHECK.getId().equals(errorOutputFileVo.getRuleTemplateId())).collect(Collectors.toList())); fileMatrixVo.setMultiTableConsistency(errorOutputFiles.stream().filter(errorOutputFileVo -> RuleTemplateName.MULTI_TABLE_ACCURACY.getId().equals(errorOutputFileVo.getRuleTemplateId())).collect(Collectors.toList())); fileMatrixVo.setFieldLengthFiles(errorOutputFiles.stream().filter(errorOutputFileVo -> RuleTemplateName.FIELD_LENGTH_CHECK.getId().equals(errorOutputFileVo.getRuleTemplateId())).collect(Collectors.toList())); fileMatrixVo.setTimelinessFiles(errorOutputFiles.stream().filter(errorOutputFileVo -> RuleTemplateName.TIMELINESS_CHECK.getId().equals(errorOutputFileVo.getRuleTemplateId())).collect(Collectors.toList()));
可以将重复的代码抽取到一个方法中,减少代码冗余,如下:
```
private List<ErrorOutputFileVo> filterErrorOutputFiles(List<ErrorOutputFileVo> errorOutputFiles, String ruleTemplateId) {
return errorOutputFiles.stream()
.filter(errorOutputFileVo -> ruleTemplateId.equals(errorOutputFileVo.getRuleTemplateId()))
.collect(Collectors.toList());
}
...
FileMatrixVo fileMatrixVo = new FileMatrixVo();
fileMatrixVo.setId(tableName + "-" + columnName);
fileMatrixVo.setCoherenceFiles(filterErrorOutputFiles(errorOutputFiles, RuleTemplateName.ENUMERATION_CHECK.getId()));
fileMatrixVo.setEffectiveFiles(filterErrorOutputFiles(errorOutputFiles, RuleTemplateName.REGEXP_CHECK.getId()));
fileMatrixVo.setCompleteFiles(filterErrorOutputFiles(errorOutputFiles, RuleTemplateName.NULL_CHECK.getId()));
fileMatrixVo.setUniquenessFiles(filterErrorOutputFiles(errorOutputFiles, RuleTemplateName.UNIQUENESS_CHECK.getId()));
fileMatrixVo.setMultiTableConsistency(filterErrorOutputFiles(errorOutputFiles, RuleTemplateName.MULTI_TABLE_ACCURACY.getId()));
fileMatrixVo.setFieldLengthFiles(filterErrorOutputFiles(errorOutputFiles, RuleTemplateName.FIELD_LENGTH_CHECK.getId()));
fileMatrixVo.setTimelinessFiles(filterErrorOutputFiles(errorOutputFiles, RuleTemplateName.TIMELINESS_CHECK.getId()));
```
阅读全文