code: private static List<Long> getAllCustomTargetingKeyIds(
        AdManagerServices adManagerServices, AdManagerSession session) throws RemoteException {
      List<Long> customTargetingKeyIds = new ArrayList<>();
  
      // Get the CustomTargetingService.
      CustomTargetingServiceInterface customTargetingService =
          adManagerServices.get(session, CustomTargetingServiceInterface.class);
  
      // Create a statement to get all custom targeting keys.
      StatementBuilder statementBuilder =
          new StatementBuilder().orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
  
      // Default for total result set size.
      int totalResultSetSize = 0;
  
      do {
        // Get custom targeting keys by statement.
        CustomTargetingKeyPage page =
            customTargetingService.getCustomTargetingKeysByStatement(statementBuilder.toStatement());
  
        if (page.getResults() != null) {
          totalResultSetSize = page.getTotalResultSetSize();
          int i = page.getStartIndex();
          for (CustomTargetingKey customTargetingKey : page.getResults()) {
            System.out.printf(
                "%d) Custom targeting key with ID %d, name '%s', and "
                    + "display name '%s' was found.%n",
                i++,
                customTargetingKey.getId(),
                customTargetingKey.getName(),
                customTargetingKey.getDisplayName());
            customTargetingKeyIds.add(customTargetingKey.getId());
          }
        }
  
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
      } while (statementBuilder.getOffset() < totalResultSetSize);
  
      return customTargetingKeyIds;
    }
docs: Gets all custom targeting key IDs.