Map. Entry <K, V> Analysis

Source: Internet
Author: User

I. Benefits

Are you tired of getting keywords from map every time and getting the corresponding values?

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 } 

 

II. Usage

Using the map. Entry class, you can get all the information at the same time. The standard MAP access method is as follows:

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    }

 

3. Analysis

1. member variables and constructors of static entry in hashmap

 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. Pay attention to the implementation of the Fast Error Reporting mechanism.

 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 entry10                 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 (); // create 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}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.