Java Reference
In-Depth Information
// Removes one mapping to the value 2 - usually inefficient and of
// limited use
m . values (). remove ( 2 );
// Remove all mappings to 4
m . values (). removeAll ( Collections . singleton ( 4 ));
// Keep only mappings to 2 & 3
m . values (). retainAll ( Arrays . asList ( 2 , 3 ));
// Deletions can also be done via iterators
Iterator < Map . Entry < String , Integer >> iter = m . entrySet (). iterator ();
while ( iter . hasNext ()) {
Map . Entry < String , Integer > e = iter . next ();
if ( e . getValue () == 2 ) iter . remove ();
}
// Find values that appear in both of two maps. In general, addAll()
// and retainAll() with keySet() and values() allow union and
// intersection
Set < Integer > v = new HashSet < Integer >( m . values ());
v . retainAll ( singleton . values ());
// Miscellaneous methods
m . clear (); // Deletes all mappings
m . size (); // Returns number of mappings: currently 0
m . isEmpty (); // Returns true
m . equals ( empty ); // true: Maps implementations override equals
The Map interface includes a variety of general-purpose and special-purpose impleā€
mentations, which are summarized in Table 8-3 . As always, complete details are in
the JDK's documentation and javadoc. All classes in Table 8-3 are in the java.util
package except ConcurrentHashMap and ConcurrentSkipListMap , which are part of
java.util.concurrent .
Table 8-3. Map implementations
Class
Representation
Since
null
keys
null
values
Notes
Hashtable
1.2
Yes
Yes
General-purpose implementation.
HashMap
Hashtable
5.0
No
No
General-purpose threadsafe
implementation; see ConcurrentMap
interface.
Concurren
tHashMap
s
a
Hashtable
6.0
No
No
Specialized threadsafe implementation;
see ConcurrentNavigableMap
interface.
Concurrent
SkipList
Map
Array
5.0
No
Yes
Keys are instances of an enum.
EnumMap
 
Search WWH ::




Custom Search