[英]Encapsulating Collection to Map transformation using Java 8's streams and collectors into a method
[英]Java 8 streams calling a method using map
我想清理每個員工對象上的一些數據。我正在嘗試這樣做,但無法實現所需的輸出。 如何使用Java8流功能?
bulkEmployeeClean( Collection<Employee> entities) {
Collection<Employee> entities2 = entities.stream().map(employee->cleanEmployee(employee));
}
Employee cleanEmployee(Employee emp) {
//clean the employee data
}
我想要的輸出是我想使用Java 8流清理每個員工對象
我收到以下錯誤消息:ErrorType不匹配:無法從void轉換為Collection
您為什么不只是迭代對象並進行修改?
bulkEmployeeClean( Collection<Employee> entities){
entities.forEach(this::cleanEmployee)
}
entities.stream()
.forEach(employee->cleanEmployee(employee))
collect(Collectors.toList())
我建議改用過濾器選項
List<Employee> activeEmployees = entities.stream().filter(employee -> employee.isActive()).collect(Collectors.toList())
這是如果您要清除Employee對象。 如果您需要在其中進行編輯,則可以對其進行迭代並在每個對象上調用clean方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.