我基本上想将此for循环转换为Observable
private static List<Book> books
public static void replaceBook(Book book) {
for (int i = 0; i < books.size(); i++) {
if (books.get(i).getId().equals(book.getId())) {
books.set(i, book);
}
}
}
这是我到目前为止所做的,但它不起作用。我认为地图操作员会更新过滤后的项目,但我似乎错过了一些东西。
Observable.from(books).filter(new Func1<Book, Boolean>() {
@Override
public Boolean call(Book b) {
return b.getId().equals(book.getId());
}
}).map(new Func1<Book, Book>() {
@Override
public Book call(Book b) {
return book;
}
}).subscribe();
更新
根据鲍里斯和塔索斯的答案,我得到了以下答案似乎有效
Observable.from(books)
.map(b -> b.getId().equals(book.getId()) ? book : b)
.toList()
.subscribe(s -> books = s);
答案 0 :(得分:2)
Trip
答案 1 :(得分:1)
Observable
.from(books)
.map(b -> b.getId().equals(book.getId()) ? book : b)
.subscribe();