import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; public class Test { public static void main(String[] args) { testHashMap(16); testHashMap(5000000); System.out.println("----------------"); testHashSet(16); testHashSet(5000000); System.out.println("----------------"); testArrayList(16); testArrayList(5000000); } public static void testHashMap(int initSize) { Map<Integer, String> map = new HashMap<Integer, String>(initSize); long beginTime = System.currentTimeMillis(); for (int i = 0; i < 5000000; i++) { map.put(i, "hellooiuytrewq"); } long endTime = System.currentTimeMillis(); System.out.println(endTime - beginTime); } public static void testHashSet(int initSize) { Set<Integer> set = new HashSet<Integer>(initSize); long beginTime = System.currentTimeMillis(); for (int i = 0; i < 5000000; i++) { set.add(i); } long endTime = System.currentTimeMillis(); System.out.println(endTime - beginTime); } public static void testArrayList(int initSize) { List<Integer> list = new ArrayList<Integer>(initSize); long beginTime = System.currentTimeMillis(); for (int i = 0; i < 5000000; i++) { list.add(i); } long endTime = System.currentTimeMillis(); System.out.println(endTime - beginTime); } }
4329
2184
----------------
2832
2088
----------------
1185
451