---
title: Real-Time Audiences for Feature Experimentation segment qualification methods for the Java SDK
slug: feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-java
docTags: 
createdAt: 2026-08-05T20:39:54.000Z
---

## Prerequisites

You must enable and configure [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8) before fetching qualified audience segments and checking if the user is qualified for the given audience segment.

## fetchQualifiedSegments

### Minimum SDK version

4.0.0

### Description

You can use the `fetchQualifiedSegments` method to retrieve the external audience mapping for a specific user from the Optimizely Data Platform (ODP) server. The Optimizely Feature Experimentation Java SDK provides a synchronous and asynchronous version of the `fetchQualifiedSegments` method.

- The caller is blocked until the synchronous fetch is completed.
- The caller of asynchronous API is not blocked.

`fetchQualifiedSegments` is a method of the `UserContext` object. See [OptimizelyUserContext](docId\:gm_UYsgiYClOVsYuAnlBX) for details.

### Parameters

The following table describes the parameters for the `fetchQualifiedSegments` method:

| Parameter                                | Type                     | Description                                                |
| ---------------------------------------- | ------------------------ | ---------------------------------------------------------- |
| \`options\` (optional)                   | `List<ODPSegmentOption>` | A set of options for fetching qualified segments from ODP. |
| \`completion \` (for asynchronous calls) | Callback function        | A completion handler to be called with the fetch result.   |

### Returns - Synchronous call

The `fetchQualifiedSegments` synchronous method returns `true` if the qualified segments array in the [user context](docId\:gm_UYsgiYClOVsYuAnlBX) was updated.

### Returns - Asynchronous call

- If the fetch completes, the Java SDK updates the qualified segments array in the [user context](docId\:gm_UYsgiYClOVsYuAnlBX) and then calls the completion handler with a success status.
- If the fetch fails, the SDK calls the handler with a failure status.

If the Java SDK does not find an ODP audience in the datafile, it returns an empty qualified segments array without sending a request to the ODP server.

:::hint{type="info"}
**Note**
You can read and write directly to the qualified segments array instead of calling `fetchQualifiedSegments`.

This lets you bypass the remote fetching process from Optimizely Data Platform ODP or use your own fetching service. This can be helpful when testing or debugging.
:::

### Example `fetchQualifiedSegments` call

The code creates a user context by instantiating an OptimizelyUserContext object with a user ID "user123" and a set of attributes, which includes an "app\_version" attribute with a value of "1.3.2".

The next section of the code shows the use of the `fetchQualifiedSegments` method to fetch all qualified segments with and without segment options.

After that, the code uses the [\`decide\`](docId:6ZSh7uRg7JxlrHwft2Ogf) method to make a decision on whether to show the feature flag with the key "flag1" to the user. Finally, the SDK calls the [\`trackEvent\`](docId\:siDJxMQqZQdP3-_90yI3o) method to track a custom event called "myevent".

```java
Map<String, Object> attributes = new HashMap<>();
attributes.put("app_version", "1.3.2");
OptimizelyUserContext user = optimizely.createUserContext("fs-id-123", attributes);

// Without segment option 
user.fetchQualifiedSegments((Boolean isFetchSuccessful) -> {
  System.out.println(isFetchSuccessful);
            
  OptimizelyDecision decision = user.decide("flag1");
  user.trackEvent("purchase_event");
});

// With segment options
List<ODPSegmentOption> odpSegmentOptions = new ArrayList<>();
odpSegmentOptions.add(ODPSegmentOption.IGNORE_CACHE);
odpSegmentOptions.add(ODPSegmentOption.RESET_CACHE);

user.fetchQualifiedSegments((Boolean isFetchSuccessful) -> {
  System.out.println(isFetchSuccessful);
            
  OptimizelyDecision decision = user.decide("flag1");
  user.trackEvent("purchase_event");
}, odpSegmentOptions);
```

```java
Map<String, Object> attributes = new HashMap<>();
attributes.put("app_version", "1.3.2");
OptimizelyUserContext user = optimizely.createUserContext("fs-id-123", attributes);

// Without segment options 
Boolean response = user.fetchQualifiedSegments();

// With segment options
List<ODPSegmentOption> odpSegmentOptions = new ArrayList<>();
odpSegmentOptions.add(ODPSegmentOption.IGNORE_CACHE);
odpSegmentOptions.add(ODPSegmentOption.RESET_CACHE);

Boolean response = user.fetchQualifiedSegments(odpSegmentOptions);

OptimizelyDecision decision = user.decide("flag1");
user.trackEvent("myevent");
```

The following diagram shows the network calls between your application, the Java SDK, and the ODP server when calling `fetchQualifiedSegments`:

![](https://api.archbee.com/api/optimize/BrbvVORKJ5iIeSMZye5kI/wJgHAcFGayn_0tHXbuQ9u_7c3bcbc628617cf57588029922716e1a2f36a3f4022beb85caf5b5dee6c0dfd1-image.png)

1. Your application calls the `FetchQualifiedSegments` method.
2. The Feature Experimentation Java SDK makes a GraphQL call to ODP to fetch segments the user ID qualifies for.
3. ODP responds with the qualified audience segments.
4. The Java SDK automatically caches the fetched segments mapping the user ID to the qualified segments.
5. The `FetchQualifiedSegments` method returns a boolean if the qualified segments array was updated.
6. Call the `Decide` method to get the flag decision for a given user, a decision object that contains the flag's status (enabled or disabled), and the assigned variation key.

The Java SDK caches any fetched segments. This means that if you request the same user segments again (when creating new user contexts), the SDK can retrieve the audience segment information from the cache rather than from the remote ODP server.

The cache is used for the `fetchQualifiedSegments` call. This method is called on the user context (the user context is fixed, including the real-time audiences the user qualifies for).

The cache *only* applies when calling the `fetchQualifiedSegments` call. If you set the cache size to 0, the cache is disabled; a cache timeout of 0 means cached entries never expire. Optimizely uses the LRU algorithm, so the oldest record is bumped out when the maximum size is reached. If there is a cache miss upon the method call, Optimizely makes a network request.

You can bypass caching using the following options with your `odpSegmentOptions` array:

- **IGNORE\_CACHE** – Bypass segments cache for lookup and save.
- **RESET\_CACHE** – Reset all segments cache.

## isQualifiedFor

### Minimum SDK Version

4.0.0

### Description

Check if the user is qualified for the given audience segment.

### Parameters

The following table describes the parameters for the `isQualifiedFor` method:

| Parameter | Type   | Description                                                          |
| --------- | ------ | -------------------------------------------------------------------- |
| `segment` | String | The ODP audience segment name to check if the user is qualified for. |

### Returns

`true` if the user is qualified.

### Examples

The following is an example of whether the user is qualified for an ODP segment:

```java
Map<String, Object> attributes = new HashMap<>();
attributes.put("laptop_os", "mac");
OptimizelyUserContext user = optimizely.createUserContext("user123", attributes);

Boolean response = user.fetchQualifiedSegments();
Boolean isQualified = user.isQualifiedFor("segment1");
```

## Source files

The language and platform source files containing the implementation for the Java SDK are available on [GitHub](https://github.com/optimizely/java-sdk/tree/4.0.0/core-api/src/main/java/com/optimizely/ab/odp).
