public double getSimilarity(FastIDSet cluster1, FastIDSet cluster2) { if (cluster1.isEmpty() || cluster2.isEmpty()) { return(Double.NaN); } double leastSimilarity = Double.PositiveInfinity; var someUsers = SamplingLongPrimitiveIterator.maybeWrapIterator(cluster1.GetEnumerator(), samplingRate); while (someUsers.MoveNext()) { long userID1 = someUsers.Current; var it2 = cluster2.GetEnumerator(); while (it2.MoveNext()) { double theSimilarity = similarity.userSimilarity(userID1, it2.Current); if (theSimilarity < leastSimilarity) { leastSimilarity = theSimilarity; } } } // We skipped everything? well, at least try comparing the first Users to get some value if (leastSimilarity == Double.PositiveInfinity) { return(similarity.userSimilarity(cluster1.GetEnumerator().Current, cluster2.GetEnumerator().Current)); } return(leastSimilarity); }