About Java Hashmap.entryset (), the document describes this: This method returns a set, which is a hashmap view, and the action on the map is reflected on the set, and vice versa. The original is
Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and Vice-versa.
This article through the source code simple analysis This function realization.
First of all, a brief introduction to the internal storage of HashMap. We know that a map is used to store key-value type data, a <k, v> the entry interface that is defined within the interface definition of a map as Entry,hashmap. HashMap maintains an entry array internally.
Transient entry[] table;
When a new element is put, the corresponding array subscript is computed according to the hash value of the key. Each element of an array is the head pointer of a linked list that is used to store the entry with the same subscript.
entry[] Table---| 0 | -> entry_0_0-> entry_0_1-> null
| 1 |-> null
...
| n-1| -> entry_n-1_0-> NULL
The EntrySet () method returns a special set, defined as the internal private class of the HashMap
Private Final class EntrySet extends abstractset<map.entry<k,v>>
Take a look at this set's iterator () method mainly. This method is simple, and returns an instance of the Entryiterator type. The Entryiterator type is a subclass of the generic hashiterator<t>, the content of which is simple, and the only code is to invoke the Hashiterator NextEntry () method in the next () function. So, the emphasis becomes the analytical nextentry () method. The above procedure is shown in the following illustration
HashMap | table
EntrySet () |iterates | hashmap.hashiterator<t> | |returns nextentry ()-Hashmap.entryset | | Iterator ()-Hashmap.entryiterator | Next ()
Hashiterator iterates through the table array to achieve the HASHMAP traversal. Internal maintenance Several variables: The index records the subscript currently in the table array, and current is used to record the position in the list currently in Table[index], and next points to the next element of present. The complete code for NextEntry () is as follows:
Final entry<k,v> NextEntry () {
if (modcount!= expectedmodcount)
throw new Concurrentmodificationexception ();
Entry<k,v> e = next;
if (E = = null)
throw new Nosuchelementexception ();
if (next = e.next) = = null) {
entry[] t = table;
while (Index < t.length && (next = t[index++]) = = null
)
;
current = e;
return e;
}
The first if is used to determine whether a concurrency error occurs in multi-threaded situations and is not discussed here for the time being. If Next is not NULL, then return and update next. The Update method is the third if: if the current list is not finished, simply move the next one back; otherwise, look for the next Non-empty slot in the table.
To sum up, the HashMap EntrySet () method returns a special set that uses Entryiterator traversal, and the iterator operates directly on the HASHMAP internal storage structure table. This approach enables the functionality of the view. The entire process does not require any secondary storage space.
P.s. From this point you can also see why EntrySet () is the most efficient way to traverse HashMap, because it is consistent with the way it is stored within HASHMAP.
Note: Effective Java says that the adapter delegates functionality to the fallback object, providing an alternative interface for the backing object. The adapter does not need to create a new instance unless it has other state information in addition to the backing object.
Ref:http://blog.sina.com.cn/s/blog_60efd9b70102vd5z.html