3
我想要一个接收DataFiles列表的方法,然后使用其谓之字段过滤其字段之一,并返回DataFiles列表而不是字符串。使用字符串谓词筛选一个流
这里是我的代码:
public static List<DataFile> someMethod(List<DataFile> dataFiles) {
return dataFiles
.stream()
.map(DataFile::getFileName)
.filter(companyAndDateFilter(date, BLABLA, LALALA))
.collect(Collectors.toList());
}
private static Predicate<String> companyAndDateFilter(LocalDate date, String... companyNames) {
String companies = getCompaniesAsString(companyNames);
String formattedDate = formatTheDate(date);
Pattern pattern = Pattern
.compile("^(" + companies + ")_(" + formattedDate + ")");
LOGGER.info("Filtering files using RegEx: " + pattern.pattern());
return pattern.asPredicate();
}
当我做.map(DataFile::getFileName)
我改变我流成Stream<String>
但我想离开它作为Stream<DataFile>
并仍然使用过滤器(这是Predicate<String>
)上DataFile::getFileName
。那可能吗?