added in version 22.1.0
belongs to Maven artifact


public class SortedList
extends Object


A Sorted list implementation that can keep items in order and also notify for changes in the list such that it can be bound to a RecyclerView.Adapter.

It keeps items ordered using the compare(Object, Object) method and uses binary search to retrieve items. If the sorting criteria of your items may change, make sure you call appropriate methods while editing them to avoid data inconsistencies.

You can control the order of items and change notifications via the SortedList.Callback parameter.


Nested classes

class SortedList.BatchedCallback<T2>

A callback implementation that can batch notify events dispatched by the SortedList. 

class SortedList.Callback<T2>

The class that controls the behavior of the SortedList



Used by indexOf(Object) when the item cannot be found in the list.

Public constructors

SortedList(Class<T> klass, Callback<T> callback)

Creates a new SortedList of type T.

SortedList(Class<T> klass, Callback<T> callback, int initialCapacity)

Creates a new SortedList of type T.

Public methods

int add(T item)

Adds the given item to the list.

void addAll(T... items)

Adds the given items to the list.

void addAll(Collection<T> items)

Adds the given items to the list.

void addAll(T[] items, boolean mayModifyInput)

Adds the given items to the list.

void beginBatchedUpdates()

Batches adapter updates that happen after calling this method and before calling endBatchedUpdates().

void clear()

Removes all items from the SortedList.

void endBatchedUpdates()

Ends the update transaction and dispatches any remaining event to the callback.

T get(int index)

Returns the item at the given index.

int indexOf(T item)

Returns the position of the provided item.

void recalculatePositionOfItemAt(int index)

This method can be used to recalculate the position of the item at the given index, without triggering an onChanged(int, int) callback.

boolean remove(T item)

Removes the provided item from the list and calls onRemoved(int, int).

T removeItemAt(int index)

Removes the item at the given index and calls onRemoved(int, int).

void replaceAll(T... items)

Replaces the current items with the new items, dispatching ListUpdateCallback events for each change detected as appropriate.