虚框代表的是接口,接口不能实例化,需要用他们的子类来实例化
Map接口可以理解为python的字典(dict)
import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class CollectionDemo { public static void main(String[] args) { Collection<String> list = new ArrayList<String>(); list.add("小明"); list.add("小王"); Iterator<String> iter=list.iterator();//生成迭代器 while(iter.hasNext()) System.out.println(iter.next()); } }
import java.util.HashMap; import java.util.Map; public class MapDemo { public static void main(String[] args) { Map<Integer, String> dict = new HashMap<Integer, String>(); dict.put(1, "a"); dict.put(2, "b"); dict.put(3, "c"); //遍历key for (Integer s : dict.keySet()) System.out.println(s); //遍历value for (String s : dict.values()) System.out.println(s); //同时遍历key和value for(Map.Entry<Integer,String> entry:dict.entrySet()) System.out.println(entry.getKey()+":"+entry.getValue()); } }