一。好处
你是否已经对每次从Map中取得关键字然后再取得相应的值感觉厌倦?
1 Set keys = map.keySet( ); 2 if(keys != null) { 3 Iterator iterator = keys.iterator( ); 4 while(iterator.hasNext( )) { 5 Object key = iterator.next( ); 6 Object value = map.get(key); 7 } 8 }
二。用法
使用Map.Entry类,你可以得到在同一时间得到所有的信息。标准的Map访问方法如下:
1 for( Entry<Integer, Integer> e : mm.entrySet()){ 2 System.out.println("key:"+e.getKey() + " value:"+e.getValue()); 3 4 } 5 for( Iterator<Entry<Integer, Integer>> i = mm.entrySet().iterator();i.hasNext(); ){ 6 Entry<Integer, Integer> e = i.next(); 7 System.out.println("key:"+e.getKey() + " value:"+e.getValue()); 8 9 }
三。分析
1.HashMap内部静态类Entry的成员变量和构造函数
1 static class Entry<K,V> implements Map.Entry<K,V> { 2 final K key; 3 V value; 4 Entry<K,V> next; 5 final int hash; 6 7 /** 8 * Creates new entry. 9 */ 10 Entry(int h, K k, V v, Entry<K,V> n) { 11 value = v; 12 next = n; 13 key = k; 14 hash = h; 15 } 16 17 。。。。。 18 }
2.Iterator
1 private final class ValueIterator extends HashIterator<V> { 2 public V next() { 3 return nextEntry().value; 4 } 5 } 6 7 private final class KeyIterator extends HashIterator<K> { 8 public K next() { 9 return nextEntry().getKey(); 10 } 11 } 12 13 private final class EntryIterator extends HashIterator<Map.Entry<K,V>> { 14 public Map.Entry<K,V> next() { 15 return nextEntry(); 16 } 17 }
3.HashIterator,注意快速报错机制实现
1 private abstract class HashIterator<E> implements Iterator<E> { 2 Entry<K,V> next; // next entry to return 3 int expectedModCount; // For fast-fail 4 int index; // current slot 5 Entry<K,V> current; // current entry 6 7 HashIterator() { 8 expectedModCount = modCount; 9 if (size > 0) { // advance to first entry 10 Entry[] t = table; 11 while (index < t.length && (next = t[index++]) == null) 12 ; 13 } 14 } 15 16 public final boolean hasNext() { 17 return next != null; 18 } 19 20 final Entry<K,V> nextEntry() { 21 if (modCount != expectedModCount) 22 throw new ConcurrentModificationException(); 23 Entry<K,V> e = next; 24 if (e == null) 25 throw new NoSuchElementException(); 26 27 if ((next = e.next) == null) { 28 Entry[] t = table; 29 while (index < t.length && (next = t[index++]) == null) 30 ; 31 } 32 current = e; 33 return e; 34 } 35 36 public void remove() { 37 if (current == null) 38 throw new IllegalStateException(); 39 if (modCount != expectedModCount) 40 throw new ConcurrentModificationException(); 41 Object k = current.key; 42 current = null; 43 HashMap.this.removeEntryForKey(k); 44 expectedModCount = modCount; 45 } 46 47 }
3.EntrySet
1 private final class EntrySet extends AbstractSet<Map.Entry<K,V>> { 2 public Iterator<Map.Entry<K,V>> iterator() { 3 return newEntryIterator(); //创建EntryIterator 4 } 5 public boolean contains(Object o) { 6 if (!(o instanceof Map.Entry)) 7 return false; 8 Map.Entry<K,V> e = (Map.Entry<K,V>) o; 9 Entry<K,V> candidate = getEntry(e.getKey()); 10 return candidate != null && candidate.equals(e); 11 } 12 public boolean remove(Object o) { 13 return removeMapping(o) != null; 14 } 15 public int size() { 16 return size; 17 } 18 public void clear() { 19 HashMap.this.clear(); 20 } 21 }