HashMap和LinkedHashMap

HashMap和LinkedHashMap都是实现Map接口,区别在于HashMap并不是按插入次序顺序存放的,而LinkedHashMap是顺序存放的

public static void  main(String[] args) {        
Map hashmap = new HashMap();   

Map linkmap  = new LinkedHashMap();    

for(int i=0;i<10;i++){      
hashmap.put(""+i, ""+i);     
linkmap.put(""+i, ""+i);    }    
System.out.println("HashMap遍历输出:");    

for(Entry entry:hashmap.entrySet()){      
System.out.print(entry.getKey()+" ");    
}    

System.out.println("");    
System.out.println("LinkedHashMap遍历输出:");    
for(Entry 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

你可能感兴趣的:(HashMap和LinkedHashMap)