/*
     * 未使用泛型面对的问题
     * 1、可以添加任何类型元素
     * 2、类型转换出现ClassCastException异常
     * 
     * 在集合中使用泛型,规定添加元素的类型
     */
    @Test
    public void test01() {
        List l = new ArrayList();
        l.add(123);
        l.add(222);
        l.add("abc");
        l.add(new a("lisi", 23));
        l.add(333);
//      java.lang.ClassCastException 异常
//      Collections.sort(l); 
        List<Integer> l1 = new ArrayList<Integer>();
        l1.add(123);
        l1.add(222);
//      l1.add("abc"); 不能添加
//      l1.add(new a("lisi", 23)); 不能添加
        l1.add(333);
        System.out.println(l1);
        TreeMap<a, Integer> m = new TreeMap<a, Integer>();
        m.put(new a("lisi",23), 10000);
        m.put(new a("zhangs",34), 20000);
        m.put(new a("lisi",55), 100000);
        m.put(new a("zhangliu",12), 2000);
        m.put(new a("wangw",45), 20000);
        Set<Map.Entry<a, Integer>> set = m.entrySet();
        Iterator<Map.Entry<a, Integer>> i = set.iterator();
        while (i.hasNext()) {
            System.out.println(i.next());
        }
    }原文:http://blog.51cto.com/f1yinsky/2136674