🔗 List Observable
Available Since 0.0.39
Introduction
A ListObservable is a specialized CollectionObservable that provides java.util.List specific operations.
Usage
@Test
void simpleList() {
ListObservable<String> names = Observable.list(); // An ArrayList<String>
names.add("Kfir");
assertEquals(List.of("Kfir"), names.get());
names.add("Lior");
assertEquals(List.of("Kfir", "Lior"), names.get());
names.addAll(List.of("Kfir", "Tom"));
assertEquals(List.of("Kfir", "Lior", "Kfir", "Tom"), names.get());
names.remove("Kfir");
assertEquals(List.of("Lior", "Kfir", "Tom"), names.get());
}
@Test
void listOrderAndMapped() {
ListObservable<Integer> scores = Observable.list();
Observable<Integer> average = scores.map(list ->
list.isEmpty()
? 0
: list.stream().reduce(0, Integer::sum) / list.size()
);
assertEquals(0, average.get());
assertEquals(List.of(), scores.get());
scores.add(100);
assertEquals(List.of(100), scores.get());
assertEquals(100, average.get());
scores.add(90);
assertEquals(List.of(100, 90), scores.get());
assertEquals(95, average.get());
scores.add(95);
assertEquals(List.of(100, 90, 95), scores.get());
assertEquals(95, average.get());
scores.add(75);
assertEquals(List.of(100, 90, 95, 75), scores.get());
assertEquals(90, average.get());
scores.sort(Integer::compareTo);
assertEquals(90, average.get());
assertEquals(List.of(75, 90, 95, 100), scores.get());
}
Replacing an Element
Available Since 0.0.53
set(int index, E element) replaces the element stored at index and returns the element that was there before — the same contract as java.util.List#set(int, Object).
@Test
void listSet() {
ListObservable<String> names = Observable.list(new ArrayList<>(List.of("Kfir", "Lior", "Tom")));
Observable<String> joined = names.map(list -> String.join(", ", list));
assertEquals("Kfir, Lior, Tom", joined.get());
// set() returns the element it replaced, just like List#set
assertEquals("Lior", names.set(1, "Voigon"));
assertEquals(List.of("Kfir", "Voigon", "Tom"), names.get());
assertEquals("Kfir, Voigon, Tom", joined.get());
// replacing an element with an equal one is a no-op: nothing changes, nobody is notified
assertEquals("Voigon", names.set(1, "Voigon"));
assertEquals(List.of("Kfir", "Voigon", "Tom"), names.get());
// set() never changes the size of the list
assertEquals(3, names.size().get());
}
Behavior
Situation | Result |
|---|
The stored element changes | The list is updated and observers are flagged dirty |
The new element equals the stored one | No-op — the list is untouched and no observer is notified |
index is out of range
| IndexOutOfBoundsException, nothing is modified or notified
|
set never changes the size of the list, so size() does not change value.
Only the element at index is affected — duplicates elsewhere in the list are left alone.
Derived, read-only views (filter, mapEach, flatMapEach, sorted, as(...)) throw UnsupportedOperationException, exactly like add(int, E), remove(int) and sort. Call set on the source list and let the change propagate down the chain.
Last modified: 23 August 2026