Java 集合
今天在做如何从两个数组中取出相同的元素时碰到了一个问题,想知道下面哪种算法更快,(听说是HashSet 的会更快,但是想知道为什么会更快呢?):
具体测试程序:
/** * get the same element in two arrays */import java.util.*;import java.io.*;public class AlgorithmTest{public static void main(String[] args)throws Exception{// read the data from fileFileReader fr = new FileReader("C:/test_data_1.txt");int temp;StringBuilder sb= new StringBuilder();while((temp=fr.read())!=-1){sb.append((char)temp);}//System.out.println(sb);String[] a = sb.toString().split(" ");Integer[] in1 = new Integer[a.length];for(int i=0;i<a.length;i++){ in1[i] = Integer.parseInt(a[i]);}Integer[] in2 = new Integer[10000];for(int i=0;i<10000;i++){in2[i] = in1[i*2];}long before,after;// Algorithm 1Set bs = new HashSet(Arrays.asList(in2));List<Integer> result = new ArrayList<Integer>();before = System.currentTimeMillis();for(int i=0;i<in1.length;i++){if(bs.contains(in1[i])){//System.out.println(a[i]);result.add(in1[i]);}}after = System.currentTimeMillis();System.out.println("HashSet Result :" + result.size());System.out.println("HashSet Algorithm using time :" + (after-before));// Algorithm 2result = new ArrayList<Integer>();before = System.currentTimeMillis();for(int i=0;i<in1.length;i++)for(int j=0;j<in2.length;j++){if(in1[i].equals(in2[j])){result.add(in1[i]);}}after = System.currentTimeMillis();System.out.println("two loops Result :" + result.size());System.out.println("two loops Algorithm using time :" + (after-before));}}其中,test data file 是用这个类生成的/** * generate the large test data */import java.io.*;public class GenTestData{public static void main(String[] args)throws Exception{ String fn = "C:/test_data_1.txt";FileWriter fs = new FileWriter(fn);for(int i=0;i<100000;i++){// 直接写字符串fs.write(i+" ");}}}private transient HashMap<E,Object> map;public boolean contains(Object o) {return map.containsKey(o);}而containsKey 方法的具体实现:/** * Returns <tt>true</tt> if this map contains a mapping for the * specified key. * * @param key The key whose presence in this map is to be tested * @return <tt>true</tt> if this map contains a mapping for the specified * key.*/public boolean containsKey(Object key) { Object k = maskNull(key); int hash = hash(k.hashCode()); int i = indexFor(hash, table.length); Entry e = table[i]; while (e != null) { if (e.hash == hash && eq(k, e.key)) return true; e = e.next; } return false;}