# Transformations

added in[version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)  
belongs to Maven artifact android.arch.lifecycle:livedata:1.1.1  
Summary:[Methods](https://developer.android.com/reference/android/arch/lifecycle/Transformations#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/arch/lifecycle/Transformations#inhmethods)  

# Transformations

| The`android.arch`Architecture Components packages are no longer maintained. They have been superseded by the corresponding[androidx.\*](https://developer.android.com/jetpack/androidx/migrate)packages. See[androidx.lifecycle.Transformations](https://developer.android.com/reference/androidx/lifecycle/Transformations)instead.

`
public


class
Transformations
`  
`

extends Object


``


`

|---|----------------------------------------|
| java.lang.Object                          ||
| ↳ | android.arch.lifecycle.Transformations |

<br />

*** ** * ** ***

Transformations for a[LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)class.

You can use transformation methods to carry information across the observer's lifecycle. The transformations aren't calculated unless an observer is observing the returned LiveData object.

Because the transformations are calculated lazily, lifecycle-related behavior is implicitly passed down without requiring additional explicit calls or dependencies.

## Summary

|                                                                                                                                                                                                                                                                                                                                                                                                                               ### Public methods                                                                                                                                                                                                                                                                                                                                                                                                                                ||
|-----------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` static <X, Y> `[LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)`<Y>` | ` `[map](https://developer.android.com/reference/android/arch/lifecycle/Transformations#map(android.arch.lifecycle.LiveData<X>, android.arch.core.util.Function<X, Y>))`(`[LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)`<X> source, `[Function](https://developer.android.com/reference/android/arch/core/util/Function)`<X, Y> func) ` Applies the given function on the main thread to each value emitted by`source`LiveData and returns LiveData, which emits resulting values.                                                                                                                                                                                                                                             |
| ` static <X, Y> `[LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)`<Y>` | ` `[switchMap](https://developer.android.com/reference/android/arch/lifecycle/Transformations#switchMap(android.arch.lifecycle.LiveData<X>, android.arch.core.util.Function<X, android.arch.lifecycle.LiveData<Y>>))`(`[LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)`<X> trigger, `[Function](https://developer.android.com/reference/android/arch/core/util/Function)`<X, `[LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)`<Y>> func) ` Creates a LiveData, let's name it`swLiveData`, which follows next flow: it reacts on changes of`trigger`LiveData, applies the given function to new value of`trigger`LiveData and sets resulting LiveData as a "backing" LiveData to`swLiveData`. |

| ### Inherited methods |
|-----------------------|---|
| 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

### map

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

```
LiveData<Y> map (LiveData<X> source, 
                Function<X, Y> func)
```

Applies the given function on the main thread to each value emitted by`source`LiveData and returns LiveData, which emits resulting values.

The given function`func`will be executed on the main thread.

Suppose that you have a LiveData, named`userLiveData`, that contains user data and you need to display the user name, created by concatenating the first and the last name of the user. You can define a function that handles the name creation, that will be applied to every value emitted by`useLiveData`.  

```
 LiveData userLiveData = ...;
 LiveData userName = Transformations.map(userLiveData, user -> {
      return user.firstName + " " + user.lastName
 });
 
```

<br />

<br />

|                      Parameters                      ||
|----------|--------------------------------------------|
| `source` | `LiveData`: a`LiveData`to listen to <br /> |
| `func`   | `Function`: a function to apply <br />     |

|                                                                 Returns                                                                  ||
|------------------------------------------------------------------------------------------|------------------------------------------------|
| [LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)`<Y>` | a LiveData which emits resulting values <br /> |

### switchMap

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

```
LiveData<Y> switchMap (LiveData<X> trigger, 
                Function<X, LiveData<Y>> func)
```

Creates a LiveData, let's name it`swLiveData`, which follows next flow: it reacts on changes of`trigger`LiveData, applies the given function to new value of`trigger`LiveData and sets resulting LiveData as a "backing" LiveData to`swLiveData`. "Backing" LiveData means, that all events emitted by it will retransmitted by`swLiveData`.

If the given function returns null, then`swLiveData`is not "backed" by any other LiveData.

The given function`func`will be executed on the main thread.

Consider the case where you have a LiveData containing a user id. Every time there's a new user id emitted, you want to trigger a request to get the user object corresponding to that id, from a repository that also returns a LiveData.

The`userIdLiveData`is the trigger and the LiveData returned by the`repository.getUserById`is the "backing" LiveData.

In a scenario where the repository contains User(1, "Jane") and User(2, "John"), when the userIdLiveData value is set to "1", the`switchMap`will call`getUser(1)`, that will return a LiveData containing the value User(1, "Jane"). So now, the userLiveData will emit User(1, "Jane"). When the user in the repository gets updated to User(1, "Sarah"), the`userLiveData`gets automatically notified and will emit User(1, "Sarah").

When the`setUserId`method is called with userId = "2", the value of the`userIdLiveData`changes and automatically triggers a request for getting the user with id "2" from the repository. So, the`userLiveData`emits User(2, "John"). The LiveData returned by`repository.getUserById(1)`is removed as a source.  

```
 MutableLiveData userIdLiveData = ...;
 LiveData userLiveData = Transformations.switchMap(userIdLiveData, id ->
     repository.getUserById(id));

 void setUserId(String userId) {
      this.userIdLiveData.setValue(userId);
 }
 
```

<br />

<br />

|                                Parameters                                 ||
|-----------|----------------------------------------------------------------|
| `trigger` | `LiveData`: a`LiveData`to listen to <br />                     |
| `func`    | `Function`: a function which creates "backing" LiveData <br /> |

|                                             Returns                                              ||
|------------------------------------------------------------------------------------------|--------|
| [LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)`<Y>` | <br /> |

-

  ## Annotations

  - [OnLifecycleEvent](https://developer.android.com/reference/android/arch/lifecycle/OnLifecycleEvent)
-

  ## Interfaces

  - [DefaultLifecycleObserver](https://developer.android.com/reference/android/arch/lifecycle/DefaultLifecycleObserver)
  - [LifecycleObserver](https://developer.android.com/reference/android/arch/lifecycle/LifecycleObserver)
  - [LifecycleOwner](https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner)
  - [LifecycleRegistryOwner](https://developer.android.com/reference/android/arch/lifecycle/LifecycleRegistryOwner)
  - [Observer](https://developer.android.com/reference/android/arch/lifecycle/Observer)
  - [ViewModelProvider.Factory](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider.Factory)
  - [ViewModelStoreOwner](https://developer.android.com/reference/android/arch/lifecycle/ViewModelStoreOwner)
-

  ## Classes

  - [AndroidViewModel](https://developer.android.com/reference/android/arch/lifecycle/AndroidViewModel)
  - [Lifecycle](https://developer.android.com/reference/android/arch/lifecycle/Lifecycle)
  - [LifecycleRegistry](https://developer.android.com/reference/android/arch/lifecycle/LifecycleRegistry)
  - [LifecycleService](https://developer.android.com/reference/android/arch/lifecycle/LifecycleService)
  - [LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)
  - [LiveDataReactiveStreams](https://developer.android.com/reference/android/arch/lifecycle/LiveDataReactiveStreams)
  - [MediatorLiveData](https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData)
  - [MutableLiveData](https://developer.android.com/reference/android/arch/lifecycle/MutableLiveData)
  - [ProcessLifecycleOwner](https://developer.android.com/reference/android/arch/lifecycle/ProcessLifecycleOwner)
  - [ServiceLifecycleDispatcher](https://developer.android.com/reference/android/arch/lifecycle/ServiceLifecycleDispatcher)
  - [Transformations](https://developer.android.com/reference/android/arch/lifecycle/Transformations)
  - [ViewModel](https://developer.android.com/reference/android/arch/lifecycle/ViewModel)
  - [ViewModelProvider](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider)
  - [ViewModelProvider.AndroidViewModelFactory](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider.AndroidViewModelFactory)
  - [ViewModelProvider.NewInstanceFactory](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider.NewInstanceFactory)
  - [ViewModelProviders](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProviders)
  - [ViewModelProviders.DefaultFactory](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProviders.DefaultFactory)
  - [ViewModelStore](https://developer.android.com/reference/android/arch/lifecycle/ViewModelStore)
  - [ViewModelStores](https://developer.android.com/reference/android/arch/lifecycle/ViewModelStores)
-

  ## Enums

  - [Lifecycle.Event](https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.Event)
  - [Lifecycle.State](https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State)