🗺️ Mapped Observable
Available Since 0.0.39
Introduction
Mapped observable is a reactive transformation layer, apply a transformation function to values from its source. This is the core of reactive mapping: taking one observable and transforming it into another value that updates automatically.
Usage
@Test
void mapped() {
MutableObservable<Integer> number = Observable.mutable(123);
Observable<Boolean> isEven = number.map(num -> num % 2 == 0);
assertFalse(isEven.get()); // because 123 is odd
number.set(8);
assertTrue(isEven.get()); // because 8 is even
number.set(11);
number.set(8);
assertTrue(isEven.get()); // because 8 is even and won't recompute the same value
Observable<String> isEvenParity = isEven.map(
b -> b
? "even"
: "odd"
);
assertEquals("even", isEvenParity.get());
number.set(16);
assertEquals("even", isEvenParity.get()); // Output "even" and isEvenParity won't be recompute because isEven at come up with same value
}
Advantages
📝 Mapped state: Ideal for mapping object into another object dynamically
👀 Lazy evaluation: Only re-computes the derived value when the base observable updates.
Last modified: 07 July 2025