[ Pobierz całość w formacie PDF ]
public int hashCode()
They take special care to deal with the possibility of having null keys and values.
You can access the key or value of the entry with the getKey() and getValue() methods, respectively:
public Object getKey()
public Object getValue()
Note Since maps support null keys and values, either getKey() or getValue() could return null.
The final method of the interface is the setValue() method, which allows you to replace the value for the key
associated with the entry:
public Object setValue(Object newValue)
The value originally associated with the key is returned from the setValue() method. If the map you are
working with is read-only, you'll get an UnsupportedOperationException thrown. There is no setKey()
method because changing the key would require the removal of the entry and addition of a new entry a set
of operations you can't do from the interface.
The nice thing about getting the set of map entries is that you don't have to do a lookup for each key if you
need to get each value, too. For instance, the following allows you to print the value of each system property
using map entries:
Properties props = System.getProperties();
Iterator iter = props.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
System.out.println(entry.getKey() + " -- " + entry.getValue());
}
Now, compare that to the code necessary to do the same thing with the older Enumeration and Properties
methods:
Properties props = System.getProperties();
Enumeration enum = props.propertyNames();
while (enum.hasMoreElements()) {
String key = (String)enum.nextElement();
System.out.println(key + " -- " + props.getProperty(key));
}
While this code may look simpler, the call to getProperty() for each key adds up to make the operation more
costly. For the Iterator version, the Map.Entry already has the property value and doesn't require an extra
lookup.
Note Remember that the historical Properties class extends from Hashtable and Hashtable was reworked to
support the Map interface.
129
HashMap Class
HashMap Class
The HashMap is the most commonly used implementation of the Map interface. It provides a basic key-value
map where the elements are unordered. If you need to maintain map keys in an ordered fashion, that's where
the TreeMap comes in handy, which will be explored later in this chapter.
The basics of the HashMap come from the AbstractMap. Table 10-3 lists its constructors and the methods it
overrides.
Table 10-3: Summary of the HashMap Class
VARIABLE/METHOD NAME VERSION DESCRIPTION
HashMap() 1.2 Constructs an empty hash map.
clear() 1.2 Removes all the elements from the hash map.
clone() 1.2 Creates a clone of the hash map.
containsKey() 1.2 Checks to see if an object is a key for the hash
map.
containsValue() 1.2 Checks to see if an object is a value within the
hash map.
entrySet() 1.2 Returns a set of key-value pairs in the hash
map.
get() 1.2 Retrieves the value for a key in the hash map.
isEmpty() 1.2 Checks if hash map has any elements.
keySet() 1.2 Retrieves a collection of the keys of the hash
map.
put() 1.2 Places a key-value pair into the hash map.
putAll() 1.2 Places a collection of key-value pairs into the
hash map.
remove() 1.2 Removes an element from the hash map.
size() 1.2 Returns the number of elements in the hash
map.
values() 1.2 Retrieves a collection of the values of the hash
map.
Basically, all of the methods of the Map interface defined in the AbstractMap class are overridden, adding in a
method from Object, too. Unlike some concrete collections, the HashMap class adds no new methods beyond
the Map interface.
Creating a HashMap
There are four constructors for creating a HashMap. The first three constructors permit creation of an empty
HashMap:
public HashMap()
public HashMap(int initialCapacity)
public HashMap(int initialCapacity, float loadFactor)
130
Adding Key-Value Pairs
Unless specified otherwise, the initial capacity of the internal data structure is 101 or 11, depending upon
which version of Java you are using. For the 1.2 release, it is 101, and for 1.3, it is 11. Unless specified
[ Pobierz całość w formacie PDF ]