Cocoa Beans 0.0.54-beta0 Help

🔗 LinkedList Observable

Available Since 0.0.50

Introduction

A LinkedListObservable is a CollectionObservable backed by a java.util.LinkedList. It combines the ordered, index-based operations of AbstractListObservable with the head/tail queue operations of QueueObservable, making it the right choice when you need both positional access and efficient FIFO/LIFO processing on an observable collection.

Creating a LinkedListObservable

// empty linked list LinkedListObservable<String> names = Observable.linkedList(); // wrap an existing LinkedList (must be modifiable) LinkedList<Integer> existing = new LinkedList<>(List.of(1, 2, 3)); LinkedListObservable<Integer> scores = Observable.linkedList(existing);

Basic Usage

@Test void linkedListExample() { LinkedListObservable<String> queue = Observable.linkedList(); // A LinkedList<String> queue.offer("Alice"); queue.offer("Bob"); queue.offer("Charlie"); assertEquals("Alice", queue.peek()); // look at head, don't remove assertEquals("Alice", queue.poll()); // remove head assertEquals("Bob", queue.poll()); assertEquals(List.of("Charlie"), queue.get()); }

Sorting and Index-based Access

Because LinkedListObservable implements ListLikeObservable, you can insert, replace or remove elements at a specific position and sort the list:

@Test void linkedListSortAndIndex() { LinkedListObservable<Integer> scores = Observable.linkedList(); scores.add(50); scores.add(90); scores.add(70); Observable<Integer> best = scores.map(list -> list.stream().max(Integer::compareTo).orElse(0)); assertEquals(90, best.get()); scores.add(0, 100); // insert at head assertEquals(List.of(100, 50, 90, 70), scores.get()); assertEquals(100, best.get()); scores.sort(Integer::compareTo); assertEquals(List.of(50, 70, 90, 100), scores.get()); }

set(int, E) replaces the element at the given index and returns the previous one. It never changes the size of the list, and — like every other mutation here — it only notifies observers when the stored element actually changes:

LinkedListObservable<String> names = Observable.linkedList(); names.addAll(List.of("Kfir", "Lior", "Tom")); String previous = names.set(1, "Voigon"); // "Lior" System.out.println(names.get()); // [Kfir, Voigon, Tom] names.set(1, "Voigon"); // equal element – observers are NOT notified

Queue Operations

LinkedListObservable also implements QueueObservable, giving you the full java.util.Queue API. See Queue Observable for details on each method.

Method

Throws on empty?

Notifies observers?

offer(E)

No

Yes (on success)

add(E)

No

Yes (on success)

poll()

No — returns null

Yes (when non-empty)

remove()

Yes — NoSuchElementException

Yes (when non-empty)

peek()

No — returns null

No

element()

Yes — NoSuchElementException

No

Observing Changes

All mutating operations (add, set, remove, offer, poll, sort, clear, …) flag downstream observables as dirty, which are recomputed lazily on the next get() call. Read-only operations (peek, element, get) never notify observers.

LinkedListObservable<Integer> queue = Observable.linkedList(); Observable<Integer> size = queue.size(); queue.offer(10); queue.offer(20); System.out.println(size.get()); // 2 queue.poll(); System.out.println(size.get()); // 1

Sort Optimisation

sort() compares the current order against the target order before sorting. If the list is already sorted by the given comparator, no observers are notified.

LinkedListObservable<Integer> list = Observable.linkedList(); list.addAll(List.of(1, 2, 3)); list.sort(Integer::compareTo); // already sorted – observers are NOT notified

Calling sort(null) always throws NullPointerException.

Last modified: 23 August 2026