我有学生列表,我需要使用更新列表进行更新。 有点像:
List<Student> students = new ArrayList<>();
List<Update> updates = new ArrayList<>();
ListIterator<Student> studentListIterator = students.listIterator();
ListIterator<Update> updateListIterator = updates.listIterator();
while (updateListIterator.hasNext()) {
Update update = updateListIterator.next();
int id = update.getId();
while (studentListIterator.hasNext()) {
Student student = studentListIterator.next();
int studentId = student.getId();
if (studentId == id) {
student.setScore(update.getDynamicScore() + 10);
}
}
}
Student.class:
public class Student {
int id;
int score;
}
更新课程:
public class Update {
private int id;
private int dynamicScore;
}
如何使用RxJava代替
答案 0 :(得分:0)
在迭代时,您必须使用LIstIterator修改列表本身。
ListIterator<Student> iter = students.listIterator();
while(iter.hasNext()){
Student std = iter.next();
// do your add/remove/edit operation here.....
}
答案 1 :(得分:0)
您需要在observable中使用operator merge来合并两个列表。 看看这个例子
/**
* Here we merge two list and we sort the list for every new item added into.
* Shall return
*
* [1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15]
*/
@Test
public void testMergeLists() {
Observable.merge(Observable.from(Arrays.asList(2, 1, 13, 11, 5)), Observable.from(Arrays.asList(10, 4, 12, 3, 14, 15)))
.collect(ArrayList<Integer>::new, ArrayList::add)
.doOnNext(Collections::sort)
.subscribe(System.out::println);
}
如果您想了解有关RxJava的更多信息,请查看此处https://github.com/politrons/reactive