欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Java中怎么循環(huán)遍歷HashMap

今天就跟大家聊聊有關(guān)Java 中怎么循環(huán)遍歷HashMap,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

成都創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務公司,擁有項目成都網(wǎng)站設計、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元石林做網(wǎng)站,已為上家服務,為石林各地企業(yè)和個人服務,聯(lián)系電話:18980820575

HashMap的三種遍歷方式

(1)for each map.entrySet()

Map<String, String> map = new HashMap<String, String>();
for (Entry<String, String> entry : map.entrySet()) {
  entry.getKey();
  entry.getValue();
}

(2)顯示調(diào)用map.entrySet()的集合迭代器

Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
  entry.getKey();
  entry.getValue();
}

(3)for each map.keySet(),再調(diào)用get獲取

Map<String, String> map = new HashMap<String, String>();
for (String key : map.keySet()) {
  map.get(key);
}

三種遍歷方式的性能測試及對比

測試環(huán)境:Windows7 32位系統(tǒng) 3.2G雙核CPU 4G內(nèi)存,Java 7,Eclipse -Xms512m -Xmx512m

測試結(jié)果:

map size10,000100,0001,000,0002,000,000
for each entrySet2ms6ms36ms91ms
for iterator entrySet0ms4ms35ms89ms
for each keySet1ms6ms48ms126ms

遍歷方式結(jié)果分析

由上表可知:

  • for each entrySet與for iterator entrySet性能等價

  • for each keySet由于要再調(diào)用get(key)獲取值,比較耗時(若hash散列算法較差,會更加耗時)

  • 在循環(huán)過程中若要對map進行刪除操作,只能用for iterator entrySet(在HahsMap非線程安全里介紹)。

HashMap entrySet源碼

private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {
  public Map.Entry<K,V> next() {
    return nextEntry();
  }
}

HashMap keySet源碼

private final class KeyIterator extends HashIterator<K> {
  public K next() {
    return nextEntry().getKey();
  }
}

由源碼可知:

keySet()與entrySet()都是返回set的迭代器。父類相同,只是返回值不同,因此性能差不多。只是keySet()多了一步根據(jù)key get value的操作而已。get的時間復雜度取決于for循環(huán)的次數(shù),即hash算法。

public V get(Object key) {
  if (key == null)
    return getForNullKey();
  Entry<K,V> entry = getEntry(key);
  return null == entry ? null : entry.getValue();
}
/**
 1. Returns the entry associated with the specified key in the
 2. HashMap. Returns null if the HashMap contains no mapping
 3. for the key.
 */
final Entry<K,V> getEntry(Object key) {
  int hash = (key == null) ? 0 : hash(key);
  for (Entry<K,V> e = table[indexFor(hash, table.length)];
     e != null;
     e = e.next) {
    Object k;
    if (e.hash == hash &&
      ((k = e.key) == key || (key != null && key.equals(k))))
      return e;
  }
  return null;
}

結(jié)論

  • 循環(huán)中需要key、value,但不對map進行刪除操作,使用for each entrySet

  • 循環(huán)中需要key、value,且要對map進行刪除操作,使用for iterator entrySet

  • 循環(huán)中只需要key,使用for each keySet

看完上述內(nèi)容,你們對Java 中怎么循環(huán)遍歷HashMap有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

當前題目:Java中怎么循環(huán)遍歷HashMap
本文鏈接:http://chinadenli.net/article34/podpse.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供面包屑導航、移動網(wǎng)站建設、網(wǎng)站排名、動態(tài)網(wǎng)站網(wǎng)站制作、用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設