# SortedList

Artifact: [androidx.recyclerview:recyclerview](https://developer.android.com/jetpack/androidx/releases/recyclerview) [View Source](https://cs.android.com/search?q=file:androidx/recyclerview/widget/SortedList.java+class:androidx.recyclerview.widget.SortedList) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

*** ** * ** ***


```
public class SortedList<T>
```

<br />

*** ** * ** ***

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 `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter`.

It keeps items ordered using the `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#compare(T2,T2)` 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 `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback` parameter.

## Summary

| ### Nested types |
|---|
| `public class https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.BatchedCallback<T2> extends https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback<T2>` A callback implementation that can batch notify events dispatched by the SortedList. |
| `public abstract class https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback<T2> implements https://developer.android.com/reference/java/util/Comparator.html<T2>, https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback` The class that controls the behavior of the `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList`. |

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#INVALID_POSITION() = -1` Used by `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#indexOf(T)` when the item cannot be found in the list. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#SortedList(java.lang.Class<T>,androidx.recyclerview.widget.SortedList.Callback<T>)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<T> klass, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback<T> callback )` Creates a new SortedList of type T. |
| `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#SortedList(java.lang.Class<T>,androidx.recyclerview.widget.SortedList.Callback<T>,int)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<T> klass, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback<T> callback, int initialCapacity )` Creates a new SortedList of type T. |

| ### Public methods |
|---|---|
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#add(T)(T item)` Adds the given item to the list. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#addAll(T...)(@https://developer.android.com/reference/androidx/annotation/NonNull T[] items)` Adds the given items to the list. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#addAll(java.util.Collection<T>)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Collection.html<T> items)` Adds the given items to the list. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#addAll(T[],boolean)(@https://developer.android.com/reference/androidx/annotation/NonNull T[] items, boolean mayModifyInput)` Adds the given items to the list. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#beginBatchedUpdates()()` Batches adapter updates that happen after calling this method and before calling `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#endBatchedUpdates()`. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#clear()()` Removes all items from the SortedList. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#endBatchedUpdates()()` Ends the update transaction and dispatches any remaining event to the callback. |
| `T` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#get(int)(int index)` Returns the item at the given index. |
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#indexOf(T)(T item)` Returns the position of the provided item. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#recalculatePositionOfItemAt(int)(int index)` This method can be used to recalculate the position of the item at the given index, without triggering an `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#onChanged(int,int)` callback. |
| `boolean` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#remove(T)(T item)` Removes the provided item from the list and calls `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onRemoved(int,int)`. |
| `T` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#removeItemAt(int)(int index)` Removes the item at the given index and calls `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onRemoved(int,int)`. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#replaceAll(T...)(@https://developer.android.com/reference/androidx/annotation/NonNull T[] items)` Replaces the current items with the new items, dispatching `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback` events for each change detected as appropriate. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#replaceAll(java.util.Collection<T>)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Collection.html<T> items)` Replaces the current items with the new items, dispatching `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback` events for each change detected as appropriate. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#replaceAll(T[],boolean)(@https://developer.android.com/reference/androidx/annotation/NonNull T[] items, boolean mayModifyInput)` Replaces the current items with the new items, dispatching `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback` events for each change detected as appropriate. |
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#size()()` The number of items in the list. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#updateItemAt(int,T)(int index, T item)` Updates the item at the given index and calls `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#onChanged(int,int)` and/or `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onMoved(int,int)` if necessary. |

## Constants

### INVALID_POSITION

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public static final int INVALID_POSITION = -1
```

Used by `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#indexOf(T)` when the item cannot be found in the list.

## Public constructors

### SortedList

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public SortedList(
    @NonNull Class<T> klass,
    @NonNull SortedList.Callback<T> callback
)
```

Creates a new SortedList of type T.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<T> klass` | The class of the contents of the SortedList. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback<T> callback` | The callback that controls the behavior of SortedList. |

### SortedList

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public SortedList(
    @NonNull Class<T> klass,
    @NonNull SortedList.Callback<T> callback,
    int initialCapacity
)
```

Creates a new SortedList of type T.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<T> klass` | The class of the contents of the SortedList. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback<T> callback` | The callback that controls the behavior of SortedList. |
| `int initialCapacity` | The initial capacity to hold items. |

## Public methods

### add

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public int add(T item)
```

Adds the given item to the list. If this is a new item, SortedList calls `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onInserted(int,int)`.

If the item already exists in the list and its sorting criteria is not changed, it is replaced with the existing Item. SortedList uses `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#areItemsTheSame(T2,T2)` to check if two items are the same item and uses `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#areContentsTheSame(T2,T2)` to decide whether it should call `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#onChanged(int,int)` or not. In both cases, it always removes the reference to the old item and puts the new item into the backing array even if `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#areContentsTheSame(T2,T2)` returns false.

If the sorting criteria of the item is changed, SortedList won't be able to find its duplicate in the list which will result in having a duplicate of the Item in the list. If you need to update sorting criteria of an item that already exists in the list, use `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#updateItemAt(int,T)`. You can find the index of the item using `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#indexOf(T)` before you update the object.

| Parameters |
|---|---|
| `T item` | The item to be added into the list. |

| Returns |
|---|---|
| `int` | The index of the newly added item. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#compare(T2,T2)` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#areItemsTheSame(T2,T2)` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#areContentsTheSame(T2,T2)` | } |

### addAll

```
public void addAll(@NonNull T[] items)
```

Adds the given items to the list. Does not modify or retain the input.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T[] items` | Array of items to be added into the list. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#addAll(T[],boolean)` |   |

### addAll

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public void addAll(@NonNull Collection<T> items)
```

Adds the given items to the list. Does not modify or retain the input.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Collection.html<T> items` | Collection of items to be added into the list. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#addAll(T[],boolean)` |   |

### addAll

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public void addAll(@NonNull T[] items, boolean mayModifyInput)
```

Adds the given items to the list. Equivalent to calling `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#add(T)` in a loop, except the callback events may be in a different order/granularity since addAll can batch them for better performance.

If allowed, will reference the input array during, and possibly after, the operation to avoid extra memory allocation, in which case you should not continue to reference or modify the array yourself.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T[] items` | Array of items to be added into the list. |
| `boolean mayModifyInput` | If true, SortedList is allowed to modify and permanently reference the input array. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#addAll(T...)` |   |

### beginBatchedUpdates

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public void beginBatchedUpdates()
```

Batches adapter updates that happen after calling this method and before calling `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#endBatchedUpdates()`. For example, if you add multiple items in a loop and they are placed into consecutive indices, SortedList calls `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onInserted(int,int)` only once with the proper item count. If an event cannot be merged with the previous event, the previous event is dispatched to the callback instantly.

After running your data updates, you **must** call `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#endBatchedUpdates()` which will dispatch any deferred data change event to the current callback.

A sample implementation may look like this:

```
    mSortedList.beginBatchedUpdates();
    try {
        mSortedList.add(item1)
        mSortedList.add(item2)
        mSortedList.remove(item3)
        ...
    } finally {
        mSortedList.endBatchedUpdates();
    }
```

Instead of using this method to batch calls, you can use a Callback that extends `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.BatchedCallback`. In that case, you must make sure that you are manually calling `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.BatchedCallback#dispatchLastEvent()` right after you complete your data changes. Failing to do so may create data inconsistencies with the Callback.

If the current Callback is an instance of `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.BatchedCallback`, calling this method has no effect.

### clear

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public void clear()
```

Removes all items from the SortedList.

### endBatchedUpdates

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public void endBatchedUpdates()
```

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

### get

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public T get(int index)
```

Returns the item at the given index.

| Parameters |
|---|---|
| `int index` | The index of the item to retrieve. |

| Returns |
|---|---|
| `T` | The item at the given index. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IndexOutOfBoundsException.html` | if provided index is negative or larger than the size of the list. |

### indexOf

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public int indexOf(T item)
```

Returns the position of the provided item.

| Parameters |
|---|---|
| `T item` | The item to query for position. |

| Returns |
|---|---|
| `int` | The position of the provided item or `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#INVALID_POSITION()` if item is not in the list. |

### recalculatePositionOfItemAt

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public void recalculatePositionOfItemAt(int index)
```

This method can be used to recalculate the position of the item at the given index, without triggering an `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#onChanged(int,int)` callback.

If you are editing objects in the list such that their position in the list may change but you don't want to trigger an onChange animation, you can use this method to re-position it. If the item changes position, SortedList will call `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onMoved(int,int)` without calling `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#onChanged(int,int)`.

A sample usage may look like:

```
    final int position = mSortedList.indexOf(item);
    item.incrementPriority(); // assume items are sorted by priority
    mSortedList.recalculatePositionOfItemAt(position);
```
In the example above, because the sorting criteria of the item has been changed, mSortedList.indexOf(item) will not be able to find the item. This is why the code above first gets the position before editing the item, edits it and informs the SortedList that item should be repositioned.

| Parameters |
|---|---|
| `int index` | The current index of the Item whose position should be re-calculated. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#updateItemAt(int,T)` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#add(T)` |   |

### remove

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public boolean remove(T item)
```

Removes the provided item from the list and calls `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onRemoved(int,int)`.

| Parameters |
|---|---|
| `T item` | The item to be removed from the list. |

| Returns |
|---|---|
| `boolean` | True if item is removed, false if item cannot be found in the list. |

### removeItemAt

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public T removeItemAt(int index)
```

Removes the item at the given index and calls `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onRemoved(int,int)`.

| Parameters |
|---|---|
| `int index` | The index of the item to be removed. |

| Returns |
|---|---|
| `T` | The removed item. |

### replaceAll

```
public void replaceAll(@NonNull T[] items)
```

Replaces the current items with the new items, dispatching `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback` events for each change detected as appropriate. Does not modify or retain the input.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T[] items` | Array of items to replace current items. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#replaceAll(T[],boolean)` |   |

### replaceAll

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public void replaceAll(@NonNull Collection<T> items)
```

Replaces the current items with the new items, dispatching `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback` events for each change detected as appropriate. Does not modify or retain the input.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Collection.html<T> items` | Array of items to replace current items. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#replaceAll(T[],boolean)` |   |

### replaceAll

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public void replaceAll(@NonNull T[] items, boolean mayModifyInput)
```

Replaces the current items with the new items, dispatching `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback` events for each change detected as appropriate.

If allowed, will reference the input array during, and possibly after, the operation to avoid extra memory allocation, in which case you should not continue to reference or modify the array yourself.

Note: this method does not detect moves or dispatch `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onMoved(int,int)` events. It instead treats moves as a remove followed by an add and therefore dispatches `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onRemoved(int,int)` and `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onRemoved(int,int)` events. See `https://developer.android.com/reference/androidx/recyclerview/widget/DiffUtil` if you want your implementation to dispatch move events.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T[] items` | Array of items to replace current items. |
| `boolean mayModifyInput` | If true, SortedList is allowed to modify and permanently reference the input array. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#replaceAll(T...)` |   |

### size

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public int size()
```

The number of items in the list.

| Returns |
|---|---|
| `int` | The number of items in the list. |

### updateItemAt

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public void updateItemAt(int index, T item)
```

Updates the item at the given index and calls `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#onChanged(int,int)` and/or `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onMoved(int,int)` if necessary.

You can use this method if you need to change an existing Item such that its position in the list may change.

If the new object is a different object (`get(index) != item`) and `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#areContentsTheSame(T2,T2)` returns `true`, SortedList avoids calling `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#onChanged(int,int)` otherwise it calls `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#onChanged(int,int)`.

If the new position of the item is different than the provided `index`, SortedList calls `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onMoved(int,int)`.

| Parameters |
|---|---|
| `int index` | The index of the item to replace |
| `T item` | The item to replace the item at the given Index. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList#add(T)` |   |