# Data

Artifact: [androidx.work:work-runtime](https://developer.android.com/jetpack/androidx/releases/work) [View Source](https://cs.android.com/search?q=file:androidx/work/Data_.kt+class:androidx.work.Data) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

*** ** * ** ***


```
public final class Data
```

<br />

*** ** * ** ***

A persistable set of key/value pairs which are used as inputs and outputs for `https://developer.android.com/reference/androidx/work/ListenableWorker`s. Keys are Strings, and values can be Strings, primitive types, or their array variants.

This is a lightweight container, and should not be considered your data store. As such, there is an enforced .MAX_DATA_BYTES limit on the serialized (byte array) size of the payloads. This class will throw `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html`s if you try to serialize or deserialize past this limit.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/work/Data.Builder` A builder for `https://developer.android.com/reference/androidx/work/Data` objects. |

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/work/Data#MAX_DATA_BYTES()` The maximum number of bytes for Data when it is serialized (converted to a byte array). |

| ### Public fields |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/Data` | `https://developer.android.com/reference/androidx/work/Data#EMPTY()` An empty Data object with no elements. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/work/Data#Data(androidx.work.Data)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/Data other)` Copy constructor |

| ### Public methods |
|---|---|
| `boolean` | `https://developer.android.com/reference/androidx/work/Data#equals(kotlin.Any)(https://developer.android.com/reference/java/lang/Object.html other)` |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/Data` | `@https://developer.android.com/reference/androidx/room/TypeConverter https://developer.android.com/reference/androidx/work/Data#fromByteArray(kotlin.ByteArray)(@https://developer.android.com/reference/androidx/annotation/NonNull byte[] bytes)` Converts a byte array to `https://developer.android.com/reference/androidx/work/Data`. |
| `final boolean` | `https://developer.android.com/reference/androidx/work/Data#getBoolean(kotlin.String,kotlin.Boolean)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, boolean defaultValue)` Gets the boolean value for the given key. |
| `final boolean[]` | `https://developer.android.com/reference/androidx/work/Data#getBooleanArray(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key)` Gets the boolean array value for the given key. |
| `final byte` | `https://developer.android.com/reference/androidx/work/Data#getByte(kotlin.String,kotlin.Byte)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, byte defaultValue)` Gets the byte value for the given key. |
| `final byte[]` | `https://developer.android.com/reference/androidx/work/Data#getByteArray(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key)` Gets the byte array value for the given key. |
| `final double` | `https://developer.android.com/reference/androidx/work/Data#getDouble(kotlin.String,kotlin.Double)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, double defaultValue)` Gets the double value for the given key. |
| `final double[]` | `https://developer.android.com/reference/androidx/work/Data#getDoubleArray(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key)` Gets the double array value for the given key. |
| `final float` | `https://developer.android.com/reference/androidx/work/Data#getFloat(kotlin.String,kotlin.Float)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, float defaultValue)` Gets the float value for the given key. |
| `final float[]` | `https://developer.android.com/reference/androidx/work/Data#getFloatArray(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key)` Gets the float array value for the given key. |
| `final int` | `https://developer.android.com/reference/androidx/work/Data#getInt(kotlin.String,kotlin.Int)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, int defaultValue)` Gets the integer value for the given key. |
| `final int[]` | `https://developer.android.com/reference/androidx/work/Data#getIntArray(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key)` Gets the integer array value for the given key. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Map.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html, https://developer.android.com/reference/java/lang/Object.html>` | `https://developer.android.com/reference/androidx/work/Data#getKeyValueMap()()` Gets all the values in this Data object. |
| `final long` | `https://developer.android.com/reference/androidx/work/Data#getLong(kotlin.String,kotlin.Long)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, long defaultValue)` Gets the long value for the given key. |
| `final long[]` | `https://developer.android.com/reference/androidx/work/Data#getLongArray(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key)` Gets the long array value for the given key. |
| `final String[]` | `https://developer.android.com/reference/androidx/work/Data#getNullableStringArray(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key)` Gets the string array value for the given key. |
| `final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/work/Data#getString(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key)` Gets the String value for the given key. |
| `final String[]` | `[getStringArray](https://developer.android.com/reference/androidx/work/Data#getStringArray(kotlin.String))(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key)` **This method is deprecated.** Use getNullableStringArray(key) instead. |
| `final boolean` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/work/Data#hasKeyWithValueOfType(kotlin.String,java.lang.Class)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> klass )` Returns `true` if the instance of `https://developer.android.com/reference/androidx/work/Data` has a non-null value corresponding to the given `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` key with the expected type of `T`. |
| `int` | `https://developer.android.com/reference/androidx/work/Data#hashCode()()` |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull byte[]` | `https://developer.android.com/reference/androidx/work/Data#toByteArray()()` Converts this Data to a byte array suitable for sending to other processes in your application. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/work/Data#toString()()` |

| ### Extension functions |
|---|---|
| `final boolean` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/work/DataKt.https://developer.android.com/reference/androidx/work/Data#(androidx.work.Data).hasKeyWithValueOfType(kotlin.String)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/Data receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key )` Returns true if the instance of `https://developer.android.com/reference/androidx/work/Data` has a value corresponding to the given `https://developer.android.com/reference/androidx/work/package-summary#(androidx.work.Data).hasKeyWithValueOfType(kotlin.String)` with an expected type `https://developer.android.com/reference/androidx/work/package-summary#(androidx.work.Data).hasKeyWithValueOfType(kotlin.String)`. |

## Constants

### MAX_DATA_BYTES

```
public static final int MAX_DATA_BYTES
```

The maximum number of bytes for Data when it is serialized (converted to a byte array). Please see the class-level Javadoc for more information.

## Public fields

### EMPTY

```
public static final @NonNull Data EMPTY
```

An empty Data object with no elements.

## Public constructors

### Data

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

```
public Data(@NonNull Data other)
```

Copy constructor

## Public methods

### equals

```
public boolean equals(Object other)
```

### fromByteArray

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

```
@TypeConverter
public static final @NonNull Data fromByteArray(@NonNull byte[] bytes)
```

Converts a byte array to `https://developer.android.com/reference/androidx/work/Data`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull byte[] bytes` | The byte array representation to convert |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/Data` | An `https://developer.android.com/reference/androidx/work/Data` object built from the input |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if bytes is bigger than .MAX_DATA_BYTES |

### getBoolean

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

```
public final boolean getBoolean(@NonNull String key, boolean defaultValue)
```

Gets the boolean value for the given key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key for the argument |
| `boolean defaultValue` | The default value to return if the key is not found |

| Returns |
|---|---|
| `boolean` | The value specified by the key if it exists; the default value otherwise |

### getBooleanArray

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

```
public final boolean[] getBooleanArray(@NonNull String key)
```

Gets the boolean array value for the given key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key for the argument |

| Returns |
|---|---|
| `boolean[]` | The value specified by the key if it exists; `null` otherwise |

### getByte

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

```
public final byte getByte(@NonNull String key, byte defaultValue)
```

Gets the byte value for the given key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key for the argument |
| `byte defaultValue` | The default value to return if the key is not found |

| Returns |
|---|---|
| `byte` | The value specified by the key if it exists; the default value otherwise |

### getByteArray

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

```
public final byte[] getByteArray(@NonNull String key)
```

Gets the byte array value for the given key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key for the argument |

| Returns |
|---|---|
| `byte[]` | The value specified by the key if it exists; `null` otherwise |

### getDouble

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

```
public final double getDouble(@NonNull String key, double defaultValue)
```

Gets the double value for the given key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key for the argument |
| `double defaultValue` | The default value to return if the key is not found |

| Returns |
|---|---|
| `double` | The value specified by the key if it exists; the default value otherwise |

### getDoubleArray

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

```
public final double[] getDoubleArray(@NonNull String key)
```

Gets the double array value for the given key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key for the argument |

| Returns |
|---|---|
| `double[]` | The value specified by the key if it exists; `null` otherwise |

### getFloat

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

```
public final float getFloat(@NonNull String key, float defaultValue)
```

Gets the float value for the given key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key for the argument |
| `float defaultValue` | The default value to return if the key is not found |

| Returns |
|---|---|
| `float` | The value specified by the key if it exists; the default value otherwise |

### getFloatArray

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

```
public final float[] getFloatArray(@NonNull String key)
```

Gets the float array value for the given key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key for the argument |

| Returns |
|---|---|
| `float[]` | The value specified by the key if it exists; `null` otherwise |

### getInt

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

```
public final int getInt(@NonNull String key, int defaultValue)
```

Gets the integer value for the given key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key for the argument |
| `int defaultValue` | The default value to return if the key is not found |

| Returns |
|---|---|
| `int` | The value specified by the key if it exists; the default value otherwise |

### getIntArray

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

```
public final int[] getIntArray(@NonNull String key)
```

Gets the integer array value for the given key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key for the argument |

| Returns |
|---|---|
| `int[]` | The value specified by the key if it exists; `null` otherwise |

### getKeyValueMap

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

```
public final @NonNull Map<@NonNull String, Object> getKeyValueMap()
```

Gets all the values in this Data object.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Map.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html, https://developer.android.com/reference/java/lang/Object.html>` | A `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-map/index.html` of key-value pairs for this object; this Map is unmodifiable and should be used for reads only. |

### getLong

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

```
public final long getLong(@NonNull String key, long defaultValue)
```

Gets the long value for the given key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key for the argument |
| `long defaultValue` | The default value to return if the key is not found |

| Returns |
|---|---|
| `long` | The value specified by the key if it exists; the default value otherwise |

### getLongArray

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

```
public final long[] getLongArray(@NonNull String key)
```

Gets the long array value for the given key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key for the argument |

| Returns |
|---|---|
| `long[]` | The value specified by the key if it exists; `null` otherwise |

### getNullableStringArray

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

```
public final String[] getNullableStringArray(@NonNull String key)
```

Gets the string array value for the given key. The array may contain `null` elements.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key for the argument |

| Returns |
|---|---|
| `String[]` | The value specified by the key if it exists; `null` otherwise |

### getString

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

```
public final String getString(@NonNull String key)
```

Gets the String value for the given key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key for the argument |

| Returns |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html` | The value specified by the key if it exists; `null` otherwise |

### getStringArray

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

```
public final String[] [getStringArray](https://developer.android.com/reference/androidx/work/Data#getStringArray(kotlin.String))(@NonNull String key)
```

> [!CAUTION]
> **This method is deprecated.**   
> Use getNullableStringArray(key) instead. This method does not correctly handle String arrays which may contain null elements, use this instead:
>
> ```
> getNullableStringArray(key)
> ```

Gets the String array value for the given key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The key for the argument |

| Returns |
|---|---|
| `String[]` | The value specified by the key if it exists; `null` otherwise |

### hasKeyWithValueOfType

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

```
public final boolean <T extends Object> hasKeyWithValueOfType(
    @NonNull String key,
    @NonNull Class<@NonNull T> klass
)
```

Returns `true` if the instance of `https://developer.android.com/reference/androidx/work/Data` has a non-null value corresponding to the given `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` key with the expected type of `T`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | The `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` key |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> klass` | The `https://developer.android.com/reference/java/lang/Class.html` container for the expected type |

| Returns |
|---|---|
| `boolean` | `true` If the instance of `https://developer.android.com/reference/androidx/work/Data` has a value for the given `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` key with the expected type. |

### hashCode

```
public int hashCode()
```

### toByteArray

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

```
public final @NonNull byte[] toByteArray()
```

Converts this Data to a byte array suitable for sending to other processes in your application. There are no versioning guarantees with this byte array, so you should not use this for IPCs between applications or persistence.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull byte[]` | The byte array representation of the input |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if the serialized payload is bigger than .MAX_DATA_BYTES |

### toString

```
public @NonNull String toString()
```

## Extension functions

### DataKt.hasKeyWithValueOfType

Artifact: [androidx.work:work-runtime](https://developer.android.com/jetpack/androidx/releases/work) [View Source](https://cs.android.com/search?q=file:androidx/work/Data.kt+function:hasKeyWithValueOfType)

```
public final boolean <T extends Object> DataKt.hasKeyWithValueOfType(
    @NonNull Data receiver,
    @NonNull String key
)
```

Returns true if the instance of `https://developer.android.com/reference/androidx/work/Data` has a value corresponding to the given `https://developer.android.com/reference/androidx/work/package-summary#(androidx.work.Data).hasKeyWithValueOfType(kotlin.String)` with an expected type `https://developer.android.com/reference/androidx/work/package-summary#(androidx.work.Data).hasKeyWithValueOfType(kotlin.String)`.