我想修改我的for循环到流:重构环路到数据流中的Android
for (TrackingGroup item : trackingGroups) {
List<IItem> items = new ArrayList<>();
items.add(new ItemText(getActivity().getString(R.string.track_title) + " " + trackingGroup.getTitle()));
for (ProfileAnswerItem answerItem : listofAnswers) {
for (TrackingItem trackingItem : item.getItems()) {
if (trackingItem.getId() == answerItem.getId()) {
ItemQuestionResult itemView = new ItemQuestionResult(new ItemAnswer(item.getId(), answerItem.getId(), answerItem.getText(), false, true),
items.size() > 1);
items.add(itemView);
// adapter.add(itemView);
} else {
if (item.getId() == answerItem.getTrackId() && answerItem.isCustom()) {
ItemQuestionResult itemView = new ItemQuestionResult(new ItemAnswer(item.getId(), answerItem.getId(), answerItem.getText(), false, true),
items.size() > 1);
if (answerItem.getId() != lastID) {
items.add(itemView);
//adapter.add(itemView);
lastID = answerItem.getId();
}
}
}
}
}
adapter.add(items);
}
,这就是我对这个时刻:
Stream.of(trackingGroups)
.forEach(trackingGroup1 -> {
List<IItem> items = new ArrayList<>();
items.add(new ItemText(getActivity().getString(R.string.track_title) + " " + trackingGroup.getTitle()));
Stream.of(listofAnswers)
.forEach(profileAnswerItem -> {
Stream.of(trackingGroup1.getItems())
.forEach(trackingItem -> {
if (trackingItem.getId() == profileAnswerItem.getId()) {
ItemQuestionResult itemView = new ItemQuestionResult(new ItemAnswer(trackingGroup1.getId(),
profileAnswerItem.getId(), profileAnswerItem.getText(), false, true),
items.size() > 1);
items.add(itemView);
} else {
if (trackingGroup1.getId() == profileAnswerItem.getTrackId() && profileAnswerItem.isCustom()) {
ItemQuestionResult itemView = new ItemQuestionResult(new ItemAnswer(trackingGroup1.getId(),
profileAnswerItem.getId(), profileAnswerItem.getText(), false, true),
items.size() > 1);
if (profileAnswerItem.getId() != lastID) {
items.add(itemView);
lastID = profileAnswerItem.getId();
}
}
}
});
});
});
现在我有一个问题变量lastID
。对于lambdas这个变量应该是final的。我如何添加缺少的部分,然后在适配器中添加结果?
没有要求使用流而不是循环。有时(如这里),代码对于流更糟糕。你为什么认为你想在这里使用流? –
我同意@AndyTurner,你*可能*可能*可能重构这个利用流的使用map/flatMap从你的数据对象到视图,但正如书面,没有什么可以通过使用流获得。 –