private <K> void serializeIndexValue(final @NotNull DataOutput out,
                                       final @NotNull StubIndexKey<K, ?> stubIndexKey,
                                       final @NotNull Map<K, StubIdList> map) throws IOException {
    final KeyDescriptor<K> keyDescriptor = StubIndexKeyDescriptorCache.INSTANCE.getKeyDescriptor(stubIndexKey);

    final BufferExposingByteArrayOutputStream indexOs = new BufferExposingByteArrayOutputStream();
    try (DataOutputStream indexDos = new DataOutputStream(indexOs)) {
      //TODO RC: what if keys (type K) are not comparable?
      final Iterable<K> keysToStore = useStableBinaryFormat ? map.keySet().stream().sorted().toList()
                                                            : map.keySet();
      for (K key : keysToStore) {
        keyDescriptor.save(indexDos, key);
        final StubIdList idsList = map.get(key);
        //TODO RC: check idsList really sorted (as its javadoc suggests)?
        StubIdExternalizer.INSTANCE.save(indexDos, idsList);
      }
      DataInputOutputUtil.writeINT(out, indexDos.size());
    }
    out.write(indexOs.getInternalBuffer(), 0, indexOs.size());
  }
private <K> void serializeIndexValue(final @NotNull DataOutput out,
                                       final @NotNull StubIndexKey<K, ?> stubIndexKey,
                                       final @NotNull Map<K, StubIdList> map)