這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)java中HashMap的使用方法,以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
HashMap簡(jiǎn)介:
1、以(鍵,值)對(duì)存儲(chǔ)數(shù)據(jù)。
2、不允許有重復(fù)的鍵,但允許有重復(fù)的值。
3、不同步(多個(gè)線程可以同時(shí)訪問(wèn))
實(shí)例演示如下:
1、添加
HashMaphash_map = new HashMap (); hash_map.put( "名字" , "anny" ); hash_map.put( "性別" , "女" ); hash_map.put( "年齡" , "20" );
2、刪除
HashMaphash_map = new HashMap (); hash_map.remove( "名字" );
3、遍歷
一般性能:
HashMaphash_map = new HashMap (); hash_map.put( "名字" , "anny" ); hash_map.put( "性別" , "女" ); hash_map.put( "年齡" , "20" ); for(String key:hash_map.keySet()) { System.out.println("Key: "+key+" Value: "+hash_map.get(key)); }
使用 Collection 類的 iterator() 方法來(lái)遍歷集合(性能好)
HashMaphash_map = new HashMap (); hash_map.put( "名字" , "anny" ); hash_map.put( "性別" , "女" ); hash_map.put( "年齡" , "20" ); Collection cl = hash_map.values(); Iterator itr = cl.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); }
上述就是小編為大家分享的java中HashMap的使用方法了,如果您也有類似的疑惑,不妨參照上述方法進(jìn)行嘗試。如果想了解更多相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊。