美文网首页
HashMap和LinkedHashMap

HashMap和LinkedHashMap

作者: C二叔 | 来源:发表于2016-10-17 13:20 被阅读0次

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

相关文章

网友评论

      本文标题:HashMap和LinkedHashMap

      本文链接:https://www.haomeiwen.com/subject/imoayttx.html