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);
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.
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