double () { if(myClusteringSearchSession.getClusters().size() == 1) return Double.NaN; Ref<Double> overallSilhouetteScore = new Ref<>(0.0); Ref<Integer> usageCount = new Ref<>(0); myClusteringSearchSession.getClusters() .forEach(cluster -> { cluster.getUsages() .forEach(usage -> { usageCount.set(usageCount.get() + 1); double cohesion = getCohesionIndex(cluster, usage); double separation = getSeparationIndex(cluster, usage); double silhouetteScore = (separation - cohesion) / Math.max(separation, cohesion); if (Double.isNaN(silhouetteScore) || Double.isInfinite(silhouetteScore)) { silhouetteScore = 0.0; } double finalSilhouetteScore = silhouetteScore; overallSilhouetteScore.set(overallSilhouetteScore.get() + finalSilhouetteScore); }); }); return overallSilhouetteScore.get() / usageCount.get(); }