import java.util.HashSet; import java.util.Arrays; import java.util.Date; public class ArraysHashcodeIssue { public static void main(String[] args) { HashSet hs = new HashSet<>(100000000,(float) 1.0); long collide = 0; long totalLoops = 0; byte[] ba = new byte[4]; System.gc(); int hash=1; long time = System.currentTimeMillis(); for(byte d=0; d<15; d++) { ba[0] = d; for(byte i=-128; i<127; i++) { ba[1] = i; for(byte k=-128; k<127; k++) { ba[2] = k; for(byte j=-128; j<127; j++) { ba[3] = j; hash = Arrays.hashCode(ba); if(hs.contains(hash)) { collide++; } else { hs.add(hash); } totalLoops++; } } } } System.out.println("total time =" + (System.currentTimeMillis() - time)); System.out.println("collisions=" + collide + " loops=" + totalLoops); System.exit(0); } }