Find distinct values (Java)

Finds the distinct values of a key for documents in a collection.

This method finds all documents that match the filter, or all documents if no filter is applied. There can be performance, latency, and billing implications if there are many matching documents.

Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart.

Result

Returns a list of the distinct values of the specified key as a Set. Documents that do not include the requested key are ignored.

Parameters

Use the distinct method, which belongs to the com.datastax.astra.client.Collection class.

Method signature
<R> Set<R> distinct(
  String key,
  Class<F> resultClass
)
<R> Set<R> distinct(
  String key,
  Filter filter,
  Class<F> resultClass
)
Name Type Summary

key

String

The name of the field for which to find values.

See the examples for usage.

filter

Filter

An object that defines filter criteria using the Data API filter syntax. The method only finds documents that match the filter criteria. Filters can improve performance by reducing the number of documents that the Data API processes.

You must use & to escape any . or & in field names in the filter clause. You cannot use & to escape any other characters. For more information, see Work with . and & in field names (Java).

For a list of available filter operators and more examples, see Filter operators for collections (Java).

Filters can use only indexed fields. If you apply selective indexing when you create a collection, you cannot reference non-indexed fields in a filter.

resultClass

Class

The type of the values that you expect this method to return.

Examples

The following examples demonstrate how to find distinct values of a key for documents in a collection.

Find distinct values of a top level field

import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.definition.documents.Document;
import java.util.Set;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        DataAPIClients.clientHCD("USERNAME", "PASSWORD")
            .getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
            .getCollection("COLLECTION_NAME");

    // Find distinct values
    Set<Integer> result = collection.distinct("publication_year", Integer.class);

    for (Integer fieldValue : result) {
      System.out.println(fieldValue);
    }
  }
}

Find distinct values of a nested field

To find distinct values for a nested field, use dot notation. For example, field.subfield.subsubfield.

You must use & to escape any literal . or & in field names.

import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.definition.documents.Document;
import java.util.Set;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        DataAPIClients.clientHCD("USERNAME", "PASSWORD")
            .getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
            .getCollection("COLLECTION_NAME");

    // Find distinct values
    Set<String> result = collection.distinct("metadata.language", String.class);

    System.out.println(result);
  }
}

Find distinct values of an index in an array

The Java client does not support dot notation or array notation to find distinct values for a specific index in an array.

Find distinct values for a subset of documents

You can use a filter to find distinct values across documents that match the filter.

For a list of available filter operators and more examples, see Filter operators for collections (Java).

Filters can use only indexed fields. If you apply selective indexing when you create a collection, you cannot reference non-indexed fields in a filter.

import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import java.util.Set;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        DataAPIClients.clientHCD("USERNAME", "PASSWORD")
            .getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
            .getCollection("COLLECTION_NAME");

    // Find distinct values
    Filter filter =
        Filters.and(Filters.eq("is_checked_out", false), Filters.lt("number_of_pages", 300));
    Set<Integer> result = collection.distinct("publication_year", filter, Integer.class);

    for (Integer fieldValue : result) {
      System.out.println(fieldValue);
    }
  }
}

Client reference

For more information, see the client reference.

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM