double (@NotNull UsageCluster currentCluster, @NotNull SimilarUsage similarUsage) { Map<UsageCluster, Double> separationIndexPerCluster = new HashMap<>(); myClusteringSearchSession.getClusters() .forEach(cluster -> { if (!cluster.getUsages().contains(similarUsage)) { cluster.getUsages() .forEach(usage -> { double similarity = jaccardDistanceExact(usage.getFeatures(), similarUsage.getFeatures()); separationIndexPerCluster.merge(cluster, similarity, (oldValue, newValue) -> oldValue + similarity); }); separationIndexPerCluster.put(cluster, separationIndexPerCluster.get(cluster) / cluster.getUsages().size()); } }); separationIndexPerCluster.put(currentCluster, 1.0); UsageCluster closestCluster = separationIndexPerCluster .entrySet() .stream() .min(Entry.comparingByValue()) .orElseThrow().getKey(); return separationIndexPerCluster.get(closestCluster); }