public static void main(String[] args) {
Map<String, String> couples = new HashMap<String, String>();
couples.put("郭靖", "黄容");
couples.put("乔峰", "阿朱");
couples.put("杨过", "小龙女");
couples.put("胡斐", "苗若兰");
System.out.println(couples);
System.out.println("使用映射项遍历Map:");
Set<Map.Entry<String, String>> entrySet = couples.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
System.out.println("使用Key遍历Map:");
Set<String> keySet = couples.keySet();
for (String key : keySet) {
System.out.println(key + " : " + couples.get(key));
}
ArrayList<String> actors = new ArrayList<String>();
actors.ensureCapacity(50);
actors.add("郭靖");
actors.add("黄容");
actors.add("乔峰");
actors.add("阿朱");
actors.trimToSize();
System.out.println("Index遍历:");
for(int i=0;i<actors.size();i++){
System.out.println(actors.get(i));
}
System.out.println("迭代器遍历:");
for(Iterator<String> it = actors.iterator();it.hasNext();){
System.out.println(it.next());
}
System.out.println("For-Each遍历:");
for(String actor:actors){
System.out.println(actor);
}
}