# SortedList

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  
belongs to Maven artifact com.android.support:recyclerview-v7:28.0.0-alpha1  
Summary:[Nested Classes](https://developer.android.com/reference/android/support/v7/util/SortedList#nestedclasses)\|[Constants](https://developer.android.com/reference/android/support/v7/util/SortedList#constants)\|[Ctors](https://developer.android.com/reference/android/support/v7/util/SortedList#pubctors)\|[Methods](https://developer.android.com/reference/android/support/v7/util/SortedList#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/v7/util/SortedList#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/v7/util/SortedList#)  

# SortedList

| This package is part of the Android[support library](https://developer.android.com/topic/libraries/support-library)which is no longer maintained. The support library has been superseded by[AndroidX](https://developer.android.com/jetpack/androidx)which is part of[Jetpack](https://developer.android.com/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider[migrating](https://developer.android.com/jetpack/androidx/migrate)existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library[class mappings](https://developer.android.com/jetpack/androidx/migrate/class-mappings).

`
public


class
SortedList
`  
`

extends Object


``


`

|---|-----------------------------------------|
| java.lang.Object                           ||
| ↳ | android.support.v7.util.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[RecyclerView.Adapter](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter).

It keeps items ordered using the[compare(Object, Object)](https://developer.android.com/reference/android/support/v7/util/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[SortedList.Callback](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback)parameter.

## Summary

|                                                                                                                       ### Nested classes                                                                                                                        ||
|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` class` | [SortedList.BatchedCallback](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback)`<T2>` A callback implementation that can batch notify events dispatched by the SortedList.                                   |
| ` class` | [SortedList.Callback](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback)`<T2>` The class that controls the behavior of the[SortedList](https://developer.android.com/reference/android/support/v7/util/SortedList). |

|                                                                                                                                  ### Constants                                                                                                                                   ||
|-------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `int` | [INVALID_POSITION](https://developer.android.com/reference/android/support/v7/util/SortedList#INVALID_POSITION) Used by[indexOf(Object)](https://developer.android.com/reference/android/support/v7/util/SortedList#indexOf(T))when the item cannot be found in the list. |

|                                                                                                                                                                            ### Public constructors                                                                                                                                                                            ||
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[SortedList](https://developer.android.com/reference/android/support/v7/util/SortedList#SortedList(java.lang.Class<T>, android.support.v7.util.SortedList.Callback<T>))`(Class<T> klass, `[Callback](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback)`<T> callback) ` Creates a new SortedList of type T.                           |
| ` `[SortedList](https://developer.android.com/reference/android/support/v7/util/SortedList#SortedList(java.lang.Class<T>, android.support.v7.util.SortedList.Callback<T>, int))`(Class<T> klass, `[Callback](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback)`<T> callback, int initialCapacity) ` Creates a new SortedList of type T. |

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

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v7/util/SortedList#)From class` java.lang.Object ` |-------------------|-------------------------------| | ` Object`         | ` clone() `                   | | ` boolean`        | ` equals(Object arg0) `       | | ` void`           | ` finalize() `                | | ` final Class<?>` | ` getClass() `                | | ` int`            | ` hashCode() `                | | ` final void`     | ` notify() `                  | | ` final void`     | ` notifyAll() `               | | ` String`         | ` toString() `                | | ` final void`     | ` wait(long arg0, int arg1) ` | | ` final void`     | ` wait(long arg0) `           | | ` final void`     | ` wait() `                    | ||

## Constants

### INVALID_POSITION

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
int INVALID_POSITION
```

Used by[indexOf(Object)](https://developer.android.com/reference/android/support/v7/util/SortedList#indexOf(T))when he item cannot be found in the list.

<br />

Constant Value: -1 (0xffffffff)

## Public constructors

### SortedList

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Creates a new SortedList of type T.

<br />

|                                      Parameters                                       ||
|------------|---------------------------------------------------------------------------|
| `klass`    | `Class`: The class of the contents of the SortedList. <br />              |
| `callback` | `Callback`: The callback that controls the behavior of SortedList. <br /> |

### SortedList

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Creates a new SortedList of type T.

<br />

|                                          Parameters                                          ||
|-------------------|---------------------------------------------------------------------------|
| `klass`           | `Class`: The class of the contents of the SortedList. <br />              |
| `callback`        | `Callback`: The callback that controls the behavior of SortedList. <br /> |
| `initialCapacity` | `int`: The initial capacity to hold items. <br />                         |

## Public methods

### add

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Adds the given item to the list. If this is a new item, SortedList calls[onInserted(int, int)](https://developer.android.com/reference/android/support/v7/util/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[areItemsTheSame(Object, Object)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#areItemsTheSame(T2, T2))to check if two items are the same item and uses[areContentsTheSame(Object, Object)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#areContentsTheSame(T2, T2))to decide whether it should call[onChanged(int, int)](https://developer.android.com/reference/android/support/v7/util/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[areContentsTheSame(Object, Object)](https://developer.android.com/reference/android/support/v7/util/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[updateItemAt(int, Object)](https://developer.android.com/reference/android/support/v7/util/SortedList#updateItemAt(int, T)). You can find the index of the item using[indexOf(Object)](https://developer.android.com/reference/android/support/v7/util/SortedList#indexOf(T))before you update the object.

<br />

|                       Parameters                        ||
|--------|-------------------------------------------------|
| `item` | `T`: The item to be added into the list. <br /> |

|                     Returns                      ||
|-------|-------------------------------------------|
| `int` | The index of the newly added item. <br /> |

**See also:**

- [compare(Object, Object)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#compare(T2, T2))
- [areItemsTheSame(Object, Object)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#areItemsTheSame(T2, T2))
- [areContentsTheSame(Object, Object)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#areContentsTheSame(T2, T2))  

### addAll

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void addAll (T... items)
```

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

<br />

|                           Parameters                           ||
|---------|-------------------------------------------------------|
| `items` | `T`: Array of items to be added into the list. <br /> |

**See also:**

- [addAll(T[], boolean)](https://developer.android.com/reference/android/support/v7/util/SortedList#addAll(T[], boolean))  

### addAll

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void addAll (Collection<T> items)
```

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

<br />

|                                  Parameters                                  ||
|---------|---------------------------------------------------------------------|
| `items` | `Collection`: Collection of items to be added into the list. <br /> |

**See also:**

- [addAll(T[], boolean)](https://developer.android.com/reference/android/support/v7/util/SortedList#addAll(T[], boolean))  

### addAll

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Adds the given items to the list. Equivalent to calling[add(T)](https://developer.android.com/reference/android/support/v7/util/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.

<br />

<br />

|                                                       Parameters                                                        ||
|------------------|-------------------------------------------------------------------------------------------------------|
| `items`          | `T`: Array of items to be added into the list. <br />                                                 |
| `mayModifyInput` | `boolean`: If true, SortedList is allowed to modify and permanently reference the input array. <br /> |

**See also:**

- [addAll(T[])](https://developer.android.com/reference/android/support/v7/util/SortedList#addAll(T...))  

### beginBatchedUpdates

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void beginBatchedUpdates ()
```

Batches adapter updates that happen after calling this method and before calling[endBatchedUpdates()](https://developer.android.com/reference/android/support/v7/util/SortedList#endBatchedUpdates()). For example, if you add multiple items in a loop and they are placed into consecutive indices, SortedList calls[onInserted(int, int)](https://developer.android.com/reference/android/support/v7/util/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[endBatchedUpdates()](https://developer.android.com/reference/android/support/v7/util/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[SortedList.BatchedCallback](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback). In that case, you must make sure that you are manually calling[dispatchLastEvent()](https://developer.android.com/reference/android/support/v7/util/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[SortedList.BatchedCallback](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback), calling this method has no effect.

<br />

### clear

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void clear ()
```

Removes all items from the SortedList.

<br />

### endBatchedUpdates

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void endBatchedUpdates ()
```

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

<br />

### get

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Returns the item at the given index.

<br />

|                        Parameters                         ||
|---------|--------------------------------------------------|
| `index` | `int`: The index of the item to retrieve. <br /> |

|                 Returns                  ||
|-----|-------------------------------------|
| `T` | The item at the given index. <br /> |

|                                             Throws                                              ||
|-----------------------------|--------------------------------------------------------------------|
| `IndexOutOfBoundsException` | if provided index is negative or larger than the size of the list. |

### indexOf

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Returns the position of the provided item.

<br />

|                     Parameters                      ||
|--------|---------------------------------------------|
| `item` | `T`: The item to query for position. <br /> |

|                                                                                           Returns                                                                                            ||
|-------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `int` | The position of the provided item or[INVALID_POSITION](https://developer.android.com/reference/android/support/v7/util/SortedList#INVALID_POSITION)if item is not in the list. <br /> |

### recalculatePositionOfItemAt

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
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)](https://developer.android.com/reference/android/support/v7/util/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[onMoved(int, int)](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback#onMoved(int, int))without calling[onChanged(int, int)](https://developer.android.com/reference/android/support/v7/util/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.

<br />

<br />

|                                          Parameters                                          ||
|---------|-------------------------------------------------------------------------------------|
| `index` | `int`: The current index of the Item whose position should be re-calculated. <br /> |

**See also:**

- [updateItemAt(int, Object)](https://developer.android.com/reference/android/support/v7/util/SortedList#updateItemAt(int, T))
- [add(Object)](https://developer.android.com/reference/android/support/v7/util/SortedList#add(T))  

### remove

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Removes the provided item from the list and calls[onRemoved(int, int)](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback#onRemoved(int, int)).

<br />

|                        Parameters                         ||
|--------|---------------------------------------------------|
| `item` | `T`: The item to be removed from the list. <br /> |

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

### removeItemAt

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Removes the item at the given index and calls[onRemoved(int, int)](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback#onRemoved(int, int)).

<br />

|                         Parameters                          ||
|---------|----------------------------------------------------|
| `index` | `int`: The index of the item to be removed. <br /> |

|            Returns            ||
|-----|--------------------------|
| `T` | The removed item. <br /> |

### replaceAll

added in[version 27.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void replaceAll (T... items)
```

Replaces the current items with the new items, dispatching[ListUpdateCallback](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback)events for each change detected as appropriate. Does not modify or retain the input.

<br />

|                          Parameters                           ||
|---------|------------------------------------------------------|
| `items` | `T`: Array of items to replace current items. <br /> |

**See also:**

- [replaceAll(T[], boolean)](https://developer.android.com/reference/android/support/v7/util/SortedList#replaceAll(T[], boolean))  

### replaceAll

added in[version 27.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void replaceAll (Collection<T> items)
```

Replaces the current items with the new items, dispatching[ListUpdateCallback](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback)events for each change detected as appropriate. Does not modify or retain the input.

<br />

|                               Parameters                               ||
|---------|---------------------------------------------------------------|
| `items` | `Collection`: Array of items to replace current items. <br /> |

**See also:**

- [replaceAll(T[], boolean)](https://developer.android.com/reference/android/support/v7/util/SortedList#replaceAll(T[], boolean))  

### replaceAll

added in[version 27.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Replaces the current items with the new items, dispatching[ListUpdateCallback](https://developer.android.com/reference/android/support/v7/util/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[onMoved(int, int)](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback#onMoved(int, int))events. It instead treats moves as a remove followed by an add and therefore dispatches[onRemoved(int, int)](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback#onRemoved(int, int))and[onRemoved(int, int)](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback#onRemoved(int, int))events. See[DiffUtil](https://developer.android.com/reference/android/support/v7/util/DiffUtil)if you want your implementation to dispatch move events.

<br />

<br />

|                                                       Parameters                                                        ||
|------------------|-------------------------------------------------------------------------------------------------------|
| `items`          | `T`: Array of items to replace current items. <br />                                                  |
| `mayModifyInput` | `boolean`: If true, SortedList is allowed to modify and permanently reference the input array. <br /> |

**See also:**

- [replaceAll(T[])](https://developer.android.com/reference/android/support/v7/util/SortedList#replaceAll(T...))  

### size

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
int size ()
```

The number of items in the list.

<br />

|                    Returns                     ||
|-------|-----------------------------------------|
| `int` | The number of items in the list. <br /> |

### updateItemAt

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Updates the item at the given index and calls[onChanged(int, int)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#onChanged(int, int))and/or[onMoved(int, int)](https://developer.android.com/reference/android/support/v7/util/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[areContentsTheSame(Object, Object)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#areContentsTheSame(T2, T2))returns`true`, SortedList avoids calling[onChanged(int, int)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#onChanged(int, int))otherwise it calls[onChanged(int, int)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#onChanged(int, int)).

If the new position of the item is different than the provided`index`, SortedList calls[onMoved(int, int)](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback#onMoved(int, int)).

<br />

|                              Parameters                               ||
|---------|--------------------------------------------------------------|
| `index` | `int`: The index of the item to replace <br />               |
| `item`  | `T`: The item to replace the item at the given Index. <br /> |

**See also:**

- [add(Object)](https://developer.android.com/reference/android/support/v7/util/SortedList#add(T))  
-

  ## Interfaces

  - [ListUpdateCallback](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback)
-

  ## Classes

  - [AdapterListUpdateCallback](https://developer.android.com/reference/android/support/v7/util/AdapterListUpdateCallback)
  - [AsyncListUtil](https://developer.android.com/reference/android/support/v7/util/AsyncListUtil)
  - [AsyncListUtil.DataCallback](https://developer.android.com/reference/android/support/v7/util/AsyncListUtil.DataCallback)
  - [AsyncListUtil.ViewCallback](https://developer.android.com/reference/android/support/v7/util/AsyncListUtil.ViewCallback)
  - [BatchingListUpdateCallback](https://developer.android.com/reference/android/support/v7/util/BatchingListUpdateCallback)
  - [DiffUtil](https://developer.android.com/reference/android/support/v7/util/DiffUtil)
  - [DiffUtil.Callback](https://developer.android.com/reference/android/support/v7/util/DiffUtil.Callback)
  - [DiffUtil.DiffResult](https://developer.android.com/reference/android/support/v7/util/DiffUtil.DiffResult)
  - [DiffUtil.ItemCallback](https://developer.android.com/reference/android/support/v7/util/DiffUtil.ItemCallback)
  - [SortedList](https://developer.android.com/reference/android/support/v7/util/SortedList)
  - [SortedList.BatchedCallback](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback)
  - [SortedList.Callback](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback)