2
轉換此嵌套迭代正在吹我的腦海有人可以將其轉換爲並行流嗎?我希望能夠檢查與我的每位客戶團隊成員的標準相符的分配。我剛剛切換到了Java 8,並且正在努力圍繞利用並行流來完成此聚合的最佳方式。將迭代器代碼轉換爲流
for(Iterator<BasicDBObject> iterator = members.iterator(); iterator.hasNext();) {
BasicDBObject member = iterator.next();
//if the member doesn't have a valid assignment remove them.
boolean memberContainsValidAssignment = false;
BasicDBObject role = member.get("accountTeamRole") == null ? null : (BasicDBObject) member.get("accountTeamRole");
if (accTeamRoleCodes != null && !accTeamRoleCodes.contains(role.get("code"))) {
iterator.remove();
continue;
}
List<BasicDBObject> assignments = member.get("assignments") == null ? new ArrayList<>() : (List) member.get("assignments");
for (Iterator<BasicDBObject> assignIterator = assignments.iterator(); assignIterator.hasNext();) {
BasicDBObject assignment = assignIterator.next();
Date endDate = (Date) assignment.get("assignmentValidToDate");
Date startDate = (Date) assignment.get("assignmentValidFromDate");
if(startDate == null){//this is junk, should have never been allowed.
LOGGER.warn("There's no start date for this assignment. {}", assignment.toString());
assignIterator.remove();
continue;
}
//1. open ended assignment, it lives on forever. 2.falls in between active date.
else if(endDate == null ||(activeDate.after(startDate) && activeDate.before(endDate))){
LOGGER.debug("adding an assignment. {}", assignment.toString());
memberContainsValidAssignment = true;
convertDatesToString(assignment);
continue;
}
}
if(!memberContainsValidAssignment){
iterator.remove();
}
}
流不會讓你從底層集合中刪除任何東西。 –
如何根據相同的標準制作新清單?我遇到的問題是三元操作和if語句。也不知道我怎麼會去做像調用轉換日期.. –
國際海事組織,你的代碼看起來很好,因爲它是。我認爲這不是Streams的任務。 – Tunaki