Java Reference
In-Depth Information
1
/**
2
* Returns the value in the map associated with the key.
3
* @param key the key to search for.
4
* @return the value that matches the key or null
5
* if the key is not found. Since null values are allowed,
6
* checking if the return value is null may not
7
* be a safe way to ascertain if the key is present in the map.
8
*/
9
public ValueType get( KeyType key )
10
{
11
Map.Entry<KeyType,ValueType> match = theSet.getMatch( makePair( key ) );
12
13
if( match == null )
14
return null;
15
else
16
return match.getValue( );
17
}
18
19
/**
20
* Adds the key value pair to the map, overriding the
21
* original value if the key was already present.
22
* @param key the key to insert.
23
* @param value the value to insert.
24
* @return the old value associated with the key, or
25
* null if the key was not present prior to this call.
26
*/
27
public ValueType put( KeyType key, ValueType value )
28
{
29
Map.Entry<KeyType,ValueType> match = theSet.getMatch( makePair( key ) );
30
31
if( match != null )
32
return match.setValue( value );
33
34
theSet.add( makePair( key, value ) );
35
return null;
36
}
37
38
/**
39
* Remove the key and its value from the map.
40
* @param key the key to remove.
41
* @return the previous value associated with the key,
42
* or null if the key was not present prior to this call.
43
*/
44
public ValueType remove( KeyType key )
45
{
46
ValueType oldValue = get( key );
47
if( oldValue != null )
48
theSet.remove( makePair( key ) );
49
50
return oldValue;
51
}
figure 19.79
Implementations of basic
MapImpl
methods
Search WWH ::
Custom Search