[ Pobierz całość w formacie PDF ]

emptiness is more efficient than manually comparing the size() to zero yourself.
When the hash map determines that it needs to increase its capacity, the private rehash() method is called.
This causes a new internal data structure to be created, inserting into it all of the values based upon the range
conversion of hash codes for the keys based on the new capacity.
132
Map Operations
Map Operations
Maps provide several operations for working with the elements of the collection. The hash map supports
fetching one of the following: a single key, all keys, all values, or all key-value pairs. You can also search for
a specific key or value within the hash table, among other tasks that are specializations of Object methods.
Fetching Keys and Values
The HashMap supports several mechanisms to retrieve keys and values out of the map thankfully, not as
many as Hashtable. The simplest is to find a value based upon a specific key with the get() method:
public Object get(Object key)
If the key is not found within the map, null is returned. Otherwise, the current value for the key within the
map is returned.
Note Remember that keys can have null values in a HashMap so get() can return null if the key was present
but has a null value.
Since objects are stored (and searched for) by the hash code of the key, you shouldn't modify the attributes of
a key object after it has been placed in a HashMap (or any map). If the hash code for an element changes after
it is in a map, the only way you'll be able to find the object is to iterate through all the elements. The get()
method will no longer function properly.
Instead of looking up the value for individual keys, you can get the set of all keys with the keySet() method:
public Set keySet()
The keySet() method returns the set of keys as a Set object.
To get the set of all the values in the hash map, call the values() method:
public Collection values()
The values() method returns a set of all values in the map as a Collection. Because values can have duplicates,
you get a Collection back instead of a Set. This means that if the same value is in the map multiple times, it
will be in the collection of values multiple times as well.
To get a set of Map.Entry elements back from a HashMap, use the entrySet() method:
public Set entrySet()
Each element of the Set returned is of type Map.Entry. When you need to work with the key-value pairs
together, this is the best of all of the mechanisms.
Finding Elements
There are two ways to check if something is in a hash map. The containsKey() method allows you to check
for the existence of a key within the map, and the containsValue() method allows you to check for a value
within the map:
133
Cloning Hash Map
public boolean containsKey(Object key)
public boolean containsValue(Object value)
The containsKey() method is like the get() method but instead of returning to you the value at the key, you get
back a boolean value stating whether or not the key is present. If found, you get true; false, otherwise. If your
map supports null values associated with keys, you would need to use containsKey() to find out if a key is in a
map, and not just get() to get the value as get() could return null if the key was found or not.
The containsValue() method checks for the existence of a specific value within the HashMap. The
containsValue() method is very inefficient as it must basically do a table scan to check for existence since
values are stored by key. If you frequently find yourself checking for values within a map, you're probably
better off maintaining two maps, one for each direction. The containsValue() method is especially inefficient
if you are frequently searching for values not in the map. To find nothing in the hash map requires checking
every element of the map.
Cloning Hash Map
The HashMap class provides its own implementation of the clone() method:
public Object clone()
This functions similarly to passing the hash map to the copy constructor of a HashMap or to creating an empty
HashMap and calling putAll().
Checking Hash Maps for Equality
You'll find the equals() method to define equality from Object overridden in the AbstractMap class: [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • zambezia2013.opx.pl
  • Pokrewne

    Start
    Cole, Allan & Chris Bunch Die Sten Chroniken 04 Division Der Verlorene
    Farmer, Philip Jose World of Tiers 04 Behind the Walls of Terra
    Lien_Merete_ _Zapomniany_ogród_04_ _Glosy_z_przeszłości
    Hardy Boys Case 04 The Lazarus Plot
    Zwiadowcy 04 Bitwa o Skandię Flanagan John
    Lowell Elizabeth Donovanowie 04 Rubinowe bagna
    Travers Pamela 04 Mary Poppins w parku
    Fremdsprache Deutsch _04
    J B Stanley [Collectible Mystery 03] A Deadly Dealer (pdf)
    Hitchcock_Alfred_ _PTD_30_ _Tajemnica_futrzanego_misia
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • psmlw.htw.pl