Skip to content

Instantly share code, notes, and snippets.

@josejuan
Last active September 10, 2021 13:17
Show Gist options
  • Save josejuan/b87ac23e255147696f8162cd09834b42 to your computer and use it in GitHub Desktop.
Save josejuan/b87ac23e255147696f8162cd09834b42 to your computer and use it in GitHub Desktop.
package org.testing;
import java.util.List;
import java.util.stream.IntStream;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;
public class Test {
public static void main(String[] args) {
// create objects and persist into memory
List<X> xs = IntStream.range(0, 1_000_000).mapToObj(n -> new X()).collect(toList());
// count hashes
System.out.println(xs.stream()
.collect(groupingBy(System::identityHashCode, counting()))
.values()
.stream()
.filter(sz -> sz > 1)
.count());
}
static class X {
}
}
/* output: 232 */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment