HashMap与LinkedHashMap比较
HashMap:
public class Test {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("三国演义", "罗贯中");
map.put("水浒传", "施耐庵");
map.put("红楼梦", "曹雪芹");
map.put("西游记", "吴承恩");
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
结果:
水浒传 = 施耐庵
三国演义 = 罗贯中
红楼梦 = 曹雪芹
西游记 = 吴承恩
LInkedHashMap:
public class Test {
public static void main(String[] args) {
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("三国演义", "罗贯中");
map.put("水浒传", "施耐庵");
map.put("红楼梦", "曹雪芹");
map.put("西游记", "吴承恩");
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
结果:
三国演义 = 罗贯中
水浒传 = 施耐庵
红楼梦 = 曹雪芹
西游记 = 吴承恩