# Java

Source: https://docs.interop.io/desktop/capabilities/data-sharing/shared-contexts/java/index.html

## Listing All Available Contexts

A shared context object is a `Map` containing cross app data. You can access all available context objects in order to manipulate them:

```java
Collection<String> names = io.contexts().names();
```

## Subscribing for Context Updates

To subscribe for shared context updates, use the `io.contexts().subscribe()` call, passing the name of the desired context object:

```java
io.contexts().subscribe("app-styling")
                .thenAccept(context -> context.data(data -> {
            // use context data here
        }));
```

If the specified shared context object doesn't exist, it will be created. In the example above, the changes to the context object can be handled in the `context.data(data -> {})` callback.

## Updating Contexts

You can also update a shared context object by using `io.contexts().update()` and passing as a first argument the name of the context object you want to update, and as a second - a `Map<String, Object>` with the delta values:

```java
io.contexts().update("app-styling", Collections.singletonMap("backgroundColor", "red"));
```

If the key you pass in the `Map` object exists in the shared context object, it will be overwritten with the new value. If it doesn't exist, it will be created. If the value of a key in the `Map` you pass to `io.contexts().update()` is `null`, then that key will be deleted from the shared context object.

Let's say you have subscribed to a shared context named `"app-styling"`, which already has two entries - `"backgroundColor", "red"` and `"alternativeColor", "yellow"`. The following example demonstrates how:

- `"backgroundColor"` will be overwritten with a new value - `"blue"`;
- `"alternativeColor"` will be deleted from the shared context object;
- `"borderColor"` will be created as a new key in the shared context object and its value will be set to `"grey"`;

```java
io.contexts()
        .subscribe("app-styling")
        .thenAccept(context -> {
            context.data(data -> {
                // use context data here
            });

            Map<String, Object> delta = new HashMap<>();
            delta.put("backgroundColor", "blue");
            delta.put("alternativeColor", null);
            delta.put("borderColor", "grey");

            context.update(delta);
        });
```
