package com.imekaku; import org.junit.Test; import java.util.*; /** * Created by lee on 2017/6/8. */ public class TestGeneric { // 不使用泛型的情况下 @Test public void test0() { List list = new ArrayList(); list.add(1); list.add(2); Iterator iterator = list.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); } for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } @Test public void test1(){ List<Integer> list = new ArrayList<Integer>(); // List<Integer> list = new ArrayList<>(); //intelliJ 中不可以写成这样 list.add(1); list.add(2); list.add(3); // 使用迭代器输出 Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } // 使用for循环输出 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } @Test public void test2() { Map<String, Integer> map = new HashMap<String, Integer>(); map.put("yi", 1); map.put("er", 2); Set<Map.Entry<String, Integer>> set = map.entrySet(); for (Map.Entry<String, Integer> m : set) { System.out.println(m.getKey() + ":" + m.getValue()); } } }