The Map Classes Java Help

The Map Classes

Several classes provide implementations of the map interfaces. The classes that can be used for maps are summarized here:

Notice that AbstractMap is a superclass rortne three cor-crete map Implementations. Weak HashMap implements a map that uses “weak keys,” which allows an clement in, a map to be garbage-collected when its key is unused. This class is not discussed further h~re. The others are deirlbed ~ext.

The HashMap Class ‘

The HashMap class ‘USesa hash table to implement the Map interface. This allows the execution time of basic operations, such as get( ) and put( ), to remain constant even for large sets. ,

The following constructors are defined:

HashMap(Map m)
HashMap(int capacity)
HashMap(int capacity, floatfillRatio)
The first form constructs a default hash map. The second form initializes the hash map by using the elements of 111. The third form initializes the capacity of the hash map to capacity. The fourth form initializes both the capacity and fill ratio of the hash map by using its arguments. The meaning of capacity and fill ratio is the same as for HashSet, described earlier.

HashMap implements Map and extends AbstradMap. It does not add any methods of its own. You should note that a hash mapdoes not guarantee the order of its elements. , Therefore, the order in which elements are added to a hash map is not necessarily the order in which they are read by an iterator, The following program illustrates HashMap. It maps names to account balances. Notice how a set-view is obtained and used.

Output from this program is shown here:
Ralph Smith: -19.08
Tom Smith: l23.22
John Doe: 3~34.34
T9dd Hall: 99.22
Jane Baker: 1378.0
John Doe’s current balance: ~434.34

The progmm begins by creating a hash map and then adds the mapping of names to balances. Next, the contents of the map are displayed by using a set-view, obtained by calling entrySet( ). The keys and values are displayed by calling the getKey( ) and gefValue( ) methods that arc defined by Map.Entry. Pay dose attention to how the deposit is made into [ohn Doc’s account. The put( ) method automatically replaces any preexisting value that is associated with the specified key with the new value. Thus, after John Doe’s updated; the hash map will still contain just one “John Doc” account.

  • Feel free to send us an inquiry, we will reply back in hours.


Posted on September 17, 2014 in java util Part 1 The Collections Framework

Share the Story

About the Author

Back to Top