# DiffUtil

added in[version 25.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/DiffUtil#nestedclasses)\|[Methods](https://developer.android.com/reference/android/support/v7/util/DiffUtil#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/v7/util/DiffUtil#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/v7/util/DiffUtil#)  

# DiffUtil

| 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
DiffUtil
`  
`

extends Object


``


`

|---|----------------------------------|
| java.lang.Object                    ||
| ↳ | android.support.v7.util.DiffUtil |

<br />

*** ** * ** ***

DiffUtil is a utility class that can calculate the difference between two lists and output a list of update operations that converts the first list into the second one.

It can be used to calculate updates for a RecyclerView Adapter. See[ListAdapter](https://developer.android.com/reference/android/support/v7/recyclerview/extensions/ListAdapter)and[AsyncListDiffer](https://developer.android.com/reference/android/support/v7/recyclerview/extensions/AsyncListDiffer)which can compute diffs using DiffUtil on a background thread.

DiffUtil uses Eugene W. Myers's difference algorithm to calculate the minimal number of updates to convert one list into another. Myers's algorithm does not handle items that are moved so DiffUtil runs a second pass on the result to detect items that were moved.

If the lists are large, this operation may take significant time so you are advised to run this on a background thread, get the[DiffUtil.DiffResult](https://developer.android.com/reference/android/support/v7/util/DiffUtil.DiffResult)then apply it on the RecyclerView on the main thread.

This algorithm is optimized for space and uses O(N) space to find the minimal number of addition and removal operations between the two lists. It has O(N + D\^2) expected time performance where D is the length of the edit script.

If move detection is enabled, it takes an additional O(N\^2) time where N is the total number of added and removed items. If your lists are already sorted by the same constraint (e.g. a created timestamp for a list of posts), you can disable move detection to improve performance.

The actual runtime of the algorithm significantly depends on the number of changes in the list and the cost of your comparison methods. Below are some average run times for reference: (The test list is composed of random UUID Strings and the tests are run on Nexus 5X with M)

- 100 items and 10 modifications: avg: 0.39 ms, median: 0.35 ms
- 100 items and 100 modifications: 3.82 ms, median: 3.75 ms
- 100 items and 100 modifications without moves: 2.09 ms, median: 2.06 ms
- 1000 items and 50 modifications: avg: 4.67 ms, median: 4.59 ms
- 1000 items and 50 modifications without moves: avg: 3.59 ms, median: 3.50 ms
- 1000 items and 200 modifications: 27.07 ms, median: 26.92 ms
- 1000 items and 200 modifications without moves: 13.54 ms, median: 13.36 ms

Due to implementation constraints, the max size of the list can be 2\^26.  
**See also:**

- [AsyncListDiffer](https://developer.android.com/reference/android/support/v7/recyclerview/extensions/AsyncListDiffer)

## Summary

|                                                                                                                                                                       ### Nested classes                                                                                                                                                                       ||
|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` class` | [DiffUtil.Callback](https://developer.android.com/reference/android/support/v7/util/DiffUtil.Callback) A Callback class used by DiffUtil while calculating the diff between two lists.                                                                                                                                                               |
| ` class` | [DiffUtil.DiffResult](https://developer.android.com/reference/android/support/v7/util/DiffUtil.DiffResult) This class holds the information about the result of a[calculateDiff(Callback, boolean)](https://developer.android.com/reference/android/support/v7/util/DiffUtil#calculateDiff(android.support.v7.util.DiffUtil.Callback, boolean))call. |
| ` class` | [DiffUtil.ItemCallback](https://developer.android.com/reference/android/support/v7/util/DiffUtil.ItemCallback)`<T>` Callback for calculating the diff between two non-null items in a list.                                                                                                                                                          |

|                                                                                                                                                                                                                                               ### Public methods                                                                                                                                                                                                                                                ||
|----------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` static `[DiffUtil.DiffResult](https://developer.android.com/reference/android/support/v7/util/DiffUtil.DiffResult) | ` `[calculateDiff](https://developer.android.com/reference/android/support/v7/util/DiffUtil#calculateDiff(android.support.v7.util.DiffUtil.Callback))`(`[DiffUtil.Callback](https://developer.android.com/reference/android/support/v7/util/DiffUtil.Callback)` cb) ` Calculates the list of update operations that can covert one list into the other one.                               |
| ` static `[DiffUtil.DiffResult](https://developer.android.com/reference/android/support/v7/util/DiffUtil.DiffResult) | ` `[calculateDiff](https://developer.android.com/reference/android/support/v7/util/DiffUtil#calculateDiff(android.support.v7.util.DiffUtil.Callback, boolean))`(`[DiffUtil.Callback](https://developer.android.com/reference/android/support/v7/util/DiffUtil.Callback)` cb, boolean detectMoves) ` Calculates the list of update operations that can covert one list into the other one. |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v7/util/DiffUtil#)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() `                    | ||

## Public methods

### calculateDiff

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

```
DiffUtil.DiffResult calculateDiff (DiffUtil.Callback cb)
```

Calculates the list of update operations that can covert one list into the other one.

<br />

|                                           Parameters                                           ||
|------|------------------------------------------------------------------------------------------|
| `cb` | `DiffUtil.Callback`: The callback that acts as a gateway to the backing list data <br /> |

|                                                                                                             Returns                                                                                                              ||
|------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------|
| [DiffUtil.DiffResult](https://developer.android.com/reference/android/support/v7/util/DiffUtil.DiffResult) | A DiffResult that contains the information about the edit sequence to convert the old list into the new list. <br /> |

### calculateDiff

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

```
DiffUtil.DiffResult calculateDiff (DiffUtil.Callback cb, 
                boolean detectMoves)
```

Calculates the list of update operations that can covert one list into the other one.

If your old and new lists are sorted by the same constraint and items never move (swap positions), you can disable move detection which takes`O(N^2)`time where N is the number of added, moved, removed items.

<br />

|                                               Parameters                                                ||
|---------------|------------------------------------------------------------------------------------------|
| `cb`          | `DiffUtil.Callback`: The callback that acts as a gateway to the backing list data <br /> |
| `detectMoves` | `boolean`: True if DiffUtil should try to detect moved items, false otherwise. <br />    |

|                                                                                                             Returns                                                                                                              ||
|------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------|
| [DiffUtil.DiffResult](https://developer.android.com/reference/android/support/v7/util/DiffUtil.DiffResult) | A DiffResult that contains the information about the edit sequence to convert the old list into the new list. <br /> |

-

  ## 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)