HashMap和LinkedHashMap都是实现Map接口,区别在于HashMap并不是按插入次序顺序存放的,而LinkedHashMap是顺序存放的
public static void main(String[] args) {
Map<String,String> hashmap = new HashMap<String,String>();
Map<String,String> linkmap = new LinkedHashMap<String,String>();
for(int i=0;i<10;i++){
hashmap.put(""+i, ""+i);
linkmap.put(""+i, ""+i); }
System.out.println("HashMap遍历输出:");
for(Entry<String,String> entry:hashmap.entrySet()){
System.out.print(entry.getKey()+" ");
}
System.out.println("");
System.out.println("LinkedHashMap遍历输出:");
for(Entry<String,String> entry:linkmap.entrySet()){
System.out.print(entry.getKey()+" "
);
}
}
HashMap遍历输出:3 2 1 0 7 6 5 4 9 8
LinkedHashMap遍历输出:0 1 2 3 4 5 6 7 8 9
网友评论