【发布时间】:2014-01-29 12:34:15
【问题描述】:
我试图找到三个不同大小的哈希集的交集。通过更改集合相交的顺序,可以找到相交的速度是否有任何差异。示例程序如下:
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));
}
}
}
【问题讨论】:
标签: java performance profiling hashset set-intersection