问题一:List存入时未指定泛型,效率影响如何?

    private static void test1() {
        int n = 1000000;

        List list = new ArrayList();
        List list1 = new ArrayList();

        for (int i = 0; i < n; i++) {
            list.add(i);
            list1.add(i);
        }

        Integer value = null;

        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            value = (Integer) list.get(i);
        }
        System.out.println("无泛型:" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            value = (Integer) list1.get(i);
        }
        System.out.println("有泛型:" + (System.currentTimeMillis() - start));
    }

无泛型:16
有泛型:0

问题二:存入时指定泛型,取出时未指定明白(List嵌套Map等)

    private static void test2() {
        int n = 10000;

        List> list = new ArrayList>();

        for (int j = 0; j < 10; j++) {
            HashMap map = new HashMap();
            for (int i = 0; i < n; i++) {
                map.put(i, i);
            }
            list.add(map);
        }

        Integer value = null;

        long start = System.currentTimeMillis();
        for (int j = 0; j < 10; j++) {
            HashMap map = list.get(j);
            for (int i = 0; i < n; i++) {
                value = (Integer) map.get(i);
            }
        }
        System.out.println("无泛型:" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        for (int j = 0; j < 10; j++) {
            HashMap map = list.get(j);
            for (int i = 0; i < n; i++) {
                value = (Integer) map.get(i);
            }
        }
        System.out.println("有泛型:" + (System.currentTimeMillis() - start));
    }

无泛型:16
有泛型:15

总结: