package zxd.work619;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class BookTest { public static void main(String[] args) { Book book1 = new Book("八仙过海", "200"); Book book2 = new Book("西游记", "500"); Book book3 = new Book("东游记", "400"); Mapmap = new HashMap ();//创建集合 map.put("1", book1); map.put("2", book2); map.put("3", book3); Set set = map.keySet(); for (String s : map.keySet()) { System.out.println(map.get(s)); } //遍历一 for (Book st : map.values()) { System.out.println(); } //遍历二 for (Map.Entry book : map.entrySet()) { System.out.println(book.getKey()+","+book.getValue()); } //遍历三 Iterator i = map.entrySet().iterator();//迭代器的调用 while(i.hasNext()){ Map.Entry m = (Map.Entry )i.next(); System.out.println(m); } }} 速度快慢:遍历三 > 遍历二 > 遍历一 输出结果:
3=Students [name=张三, age=22]2=Students [name=张二, age=21]1=Students [name=张一, age=20]5=Students [name=张五, age=24]4=Students [name=张四, age=23]