さまざまなサイズの 3 つのハッシュセットの共通部分を見つけようとしています。セットが交差する順序を変更することによって、交差を見つけることができる速度に違いはありますか? プログラムの例は次のようになります。
public class RetainTest {
static Set<Integer> large =new HashSet<>();
static Set<Integer> medium =new HashSet<>();
static Set<Integer> small =new HashSet<>();
static int largeSize=10000;
static int midSize=5000;
static int smallSize=1000;
public static void main(String[] args){
preamble()
large.retainAll(medium);
large.retainAll(small);
System.out.println(large.size());
}
public static void preamble(){
large =new HashSet<>();
medium =new HashSet<>();
small =new HashSet<>();
Random rnd=new Random(15);
for(int i=0;i<largeSize;i++){
large.add(rnd.nextInt(largeSize*10));
}
for(int i=0;i<midSize;i++){
medium.add(rnd.nextInt(largeSize*10));
}
for(int i=0;i<smallSize;i++){
small.add(rnd.nextInt(largeSize*10));
}
}
}
