# BundleCollectionUtil

Artifact: [androidx.media3:media3-common](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/common/util/BundleCollectionUtil.java+class:androidx.media3.common.util.BundleCollectionUtil)

*** ** * ** ***


```
@UnstableApi
public final class BundleCollectionUtil
```

<br />

*** ** * ** ***

Utilities for converting collections to and from `https://developer.android.com/reference/android/os/Bundle.html` instances.

## Summary

| ### Public methods |
|---|---|
| `static https://developer.android.com/reference/java/util/HashMap.html<https://developer.android.com/reference/java/lang/String.html, https://developer.android.com/reference/java/lang/String.html>` | `https://developer.android.com/reference/androidx/media3/common/util/BundleCollectionUtil#bundleToStringHashMap(android.os.Bundle)(https://developer.android.com/reference/android/os/Bundle.html bundle)` |
| `static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableMap.html<https://developer.android.com/reference/java/lang/String.html, https://developer.android.com/reference/java/lang/String.html>` | `https://developer.android.com/reference/androidx/media3/common/util/BundleCollectionUtil#bundleToStringImmutableMap(android.os.Bundle)(https://developer.android.com/reference/android/os/Bundle.html bundle)` |
| `static void` | `https://developer.android.com/reference/androidx/media3/common/util/BundleCollectionUtil#ensureClassLoader(android.os.Bundle)(@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/os/Bundle.html bundle)` Sets the application class loader to the given `https://developer.android.com/reference/android/os/Bundle.html` if no class loader is present. |
| `static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<T>` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/media3/common/util/BundleCollectionUtil#fromBundleList(java.util.function.Function<android.os.Bundle,T>,java.util.List<android.os.Bundle>)( https://developer.android.com/reference/java/util/function/Function.html<https://developer.android.com/reference/android/os/Bundle.html, T> fromBundleFunc, https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/android/os/Bundle.html> bundleList )` Unbundles a list of `https://developer.android.com/reference/android/os/Bundle.html` instances to a list of objects. |
| `static https://developer.android.com/reference/android/util/SparseArray.html<T>` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/media3/common/util/BundleCollectionUtil#fromBundleSparseArray(java.util.function.Function<android.os.Bundle,T>,android.util.SparseArray<android.os.Bundle>)( https://developer.android.com/reference/java/util/function/Function.html<https://developer.android.com/reference/android/os/Bundle.html, T> fromBundleFunc, https://developer.android.com/reference/android/util/SparseArray.html<https://developer.android.com/reference/android/os/Bundle.html> bundleSparseArray )` Unbundles a `https://developer.android.com/reference/android/util/SparseArray.html` of `https://developer.android.com/reference/android/os/Bundle.html` instances to a `https://developer.android.com/reference/android/util/SparseArray.html` of objects. |
| `static https://developer.android.com/reference/android/os/Bundle.html` | `https://developer.android.com/reference/androidx/media3/common/util/BundleCollectionUtil#getBundleWithDefault(android.os.Bundle,java.lang.String,android.os.Bundle)(https://developer.android.com/reference/android/os/Bundle.html bundle, https://developer.android.com/reference/java/lang/String.html field, https://developer.android.com/reference/android/os/Bundle.html defaultValue)` |
| `static https://developer.android.com/reference/java/util/ArrayList.html<https://developer.android.com/reference/java/lang/Integer.html>` | `https://developer.android.com/reference/androidx/media3/common/util/BundleCollectionUtil#getIntegerArrayListWithDefault(android.os.Bundle,java.lang.String,java.util.ArrayList<java.lang.Integer>)( https://developer.android.com/reference/android/os/Bundle.html bundle, https://developer.android.com/reference/java/lang/String.html field, https://developer.android.com/reference/java/util/ArrayList.html<https://developer.android.com/reference/java/lang/Integer.html> defaultValue )` |
| `static https://developer.android.com/reference/android/os/Bundle.html` | `https://developer.android.com/reference/androidx/media3/common/util/BundleCollectionUtil#stringMapToBundle(java.util.Map<java.lang.String,java.lang.String>)(https://developer.android.com/reference/java/util/Map.html<https://developer.android.com/reference/java/lang/String.html, https://developer.android.com/reference/java/lang/String.html> map)` |
| `static https://developer.android.com/reference/java/util/ArrayList.html<https://developer.android.com/reference/android/os/Bundle.html>` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/media3/common/util/BundleCollectionUtil#toBundleArrayList(java.util.Collection<T>,java.util.function.Function<T,android.os.Bundle>)( https://developer.android.com/reference/java/util/Collection.html<T> items, https://developer.android.com/reference/java/util/function/Function.html<T, https://developer.android.com/reference/android/os/Bundle.html> toBundleFunc )` Bundles a collection of objects to an `https://developer.android.com/reference/java/util/ArrayList.html` of `https://developer.android.com/reference/android/os/Bundle.html` instances so that the returned list can be put to `https://developer.android.com/reference/android/os/Bundle.html` using `https://developer.android.com/reference/android/os/Bundle.html#putParcelableArrayList(java.lang.String, java.util.ArrayList&lt;? extends android.os.Parcelable&gt;)` conveniently. |
| `static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/android/os/Bundle.html>` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/media3/common/util/BundleCollectionUtil#toBundleList(java.util.List<T>,java.util.function.Function<T,android.os.Bundle>)(https://developer.android.com/reference/java/util/List.html<T> list, https://developer.android.com/reference/java/util/function/Function.html<T, https://developer.android.com/reference/android/os/Bundle.html> toBundleFunc)` Bundles a list of objects to a list of `https://developer.android.com/reference/android/os/Bundle.html` instances. |
| `static https://developer.android.com/reference/android/util/SparseArray.html<https://developer.android.com/reference/android/os/Bundle.html>` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/media3/common/util/BundleCollectionUtil#toBundleSparseArray(android.util.SparseArray<T>,java.util.function.Function<T,android.os.Bundle>)( https://developer.android.com/reference/android/util/SparseArray.html<T> items, https://developer.android.com/reference/java/util/function/Function.html<T, https://developer.android.com/reference/android/os/Bundle.html> toBundleFunc )` Bundles a `https://developer.android.com/reference/android/util/SparseArray.html` of objects to a `https://developer.android.com/reference/android/util/SparseArray.html` of `https://developer.android.com/reference/android/os/Bundle.html` instances so that the returned `https://developer.android.com/reference/android/util/SparseArray.html` can be put to `https://developer.android.com/reference/android/os/Bundle.html` using `https://developer.android.com/reference/android/os/Bundle.html#putSparseParcelableArray(java.lang.String, android.util.SparseArray&lt;? extends android.os.Parcelable&gt;)` conveniently. |

## Public methods

### bundleToStringHashMap

```
public static HashMap<String, String> bundleToStringHashMap(Bundle bundle)
```

### bundleToStringImmutableMap

```
public static ImmutableMap<String, String> bundleToStringImmutableMap(Bundle bundle)
```

### ensureClassLoader

```
public static void ensureClassLoader(@Nullable Bundle bundle)
```

Sets the application class loader to the given `https://developer.android.com/reference/android/os/Bundle.html` if no class loader is present.

This assumes that all classes unparceled from `bundle` are sharing the class loader of `BundleCollectionUtil`.

### fromBundleList

```
public static ImmutableList<T> <T extends Object> fromBundleList(
    Function<Bundle, T> fromBundleFunc,
    List<Bundle> bundleList
)
```

Unbundles a list of `https://developer.android.com/reference/android/os/Bundle.html` instances to a list of objects.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/util/function/Function.html<https://developer.android.com/reference/android/os/Bundle.html, T> fromBundleFunc` | Function that specified how to unbundle each item. |
| `https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/android/os/Bundle.html> bundleList` | List of `https://developer.android.com/reference/android/os/Bundle.html` instances to be unbundled. |

| Returns |
|---|---|
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<T>` | The `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html` of unbundled items. |

### fromBundleSparseArray

```
public static SparseArray<T> <T extends Object> fromBundleSparseArray(
    Function<Bundle, T> fromBundleFunc,
    SparseArray<Bundle> bundleSparseArray
)
```

Unbundles a `https://developer.android.com/reference/android/util/SparseArray.html` of `https://developer.android.com/reference/android/os/Bundle.html` instances to a `https://developer.android.com/reference/android/util/SparseArray.html` of objects.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/util/function/Function.html<https://developer.android.com/reference/android/os/Bundle.html, T> fromBundleFunc` | Function that specified how to unbundle each item. |
| `https://developer.android.com/reference/android/util/SparseArray.html<https://developer.android.com/reference/android/os/Bundle.html> bundleSparseArray` | `https://developer.android.com/reference/android/util/SparseArray.html` of `https://developer.android.com/reference/android/os/Bundle.html` instances to be unbundled. |

| Returns |
|---|---|
| `https://developer.android.com/reference/android/util/SparseArray.html<T>` | The `https://developer.android.com/reference/android/util/SparseArray.html` of unbundled items. |

### getBundleWithDefault

```
public static Bundle getBundleWithDefault(Bundle bundle, String field, Bundle defaultValue)
```

### getIntegerArrayListWithDefault

```
public static ArrayList<Integer> getIntegerArrayListWithDefault(
    Bundle bundle,
    String field,
    ArrayList<Integer> defaultValue
)
```

### stringMapToBundle

```
public static Bundle stringMapToBundle(Map<String, String> map)
```

### toBundleArrayList

```
public static ArrayList<Bundle> <T extends Object> toBundleArrayList(
    Collection<T> items,
    Function<T, Bundle> toBundleFunc
)
```

Bundles a collection of objects to an `https://developer.android.com/reference/java/util/ArrayList.html` of `https://developer.android.com/reference/android/os/Bundle.html` instances so that the returned list can be put to `https://developer.android.com/reference/android/os/Bundle.html` using `https://developer.android.com/reference/android/os/Bundle.html#putParcelableArrayList(java.lang.String, java.util.ArrayList&lt;? extends android.os.Parcelable&gt;)` conveniently.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/util/Collection.html<T> items` | Collection of items to be bundled. |
| `https://developer.android.com/reference/java/util/function/Function.html<T, https://developer.android.com/reference/android/os/Bundle.html> toBundleFunc` | Function that specifies how to bundle each item. |

| Returns |
|---|---|
| `https://developer.android.com/reference/java/util/ArrayList.html<https://developer.android.com/reference/android/os/Bundle.html>` | The `https://developer.android.com/reference/java/util/ArrayList.html` of bundled items. |

### toBundleList

```
public static ImmutableList<Bundle> <T extends Object> toBundleList(List<T> list, Function<T, Bundle> toBundleFunc)
```

Bundles a list of objects to a list of `https://developer.android.com/reference/android/os/Bundle.html` instances.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/util/List.html<T> list` | List of items to be bundled. |
| `https://developer.android.com/reference/java/util/function/Function.html<T, https://developer.android.com/reference/android/os/Bundle.html> toBundleFunc` | Function that specifies how to bundle each item. |

| Returns |
|---|---|
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/android/os/Bundle.html>` | The `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html` of bundled items. |

### toBundleSparseArray

```
public static SparseArray<Bundle> <T extends Object> toBundleSparseArray(
    SparseArray<T> items,
    Function<T, Bundle> toBundleFunc
)
```

Bundles a `https://developer.android.com/reference/android/util/SparseArray.html` of objects to a `https://developer.android.com/reference/android/util/SparseArray.html` of `https://developer.android.com/reference/android/os/Bundle.html` instances so that the returned `https://developer.android.com/reference/android/util/SparseArray.html` can be put to `https://developer.android.com/reference/android/os/Bundle.html` using `https://developer.android.com/reference/android/os/Bundle.html#putSparseParcelableArray(java.lang.String, android.util.SparseArray&lt;? extends android.os.Parcelable&gt;)` conveniently.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/util/SparseArray.html<T> items` | Collection of items to be bundled. |
| `https://developer.android.com/reference/java/util/function/Function.html<T, https://developer.android.com/reference/android/os/Bundle.html> toBundleFunc` | Function that specifies how to bundle each item. |

| Returns |
|---|---|
| `https://developer.android.com/reference/android/util/SparseArray.html<https://developer.android.com/reference/android/os/Bundle.html>` | The `https://developer.android.com/reference/android/util/SparseArray.html` of bundled items. |