Java Reference
In-Depth Information
ized Hashtable class (in most cases, but do refer to the documentation for some edge cases
that need consideration). Using it where suitable is as simple as adding an import and chan-
ging:
Map myMap = new Hashtable();
to
Map myMap = new ConcurrentHashMap();
Of course, because you read the section on Generics (see Java 5 generic types ) , you'll know
that you probably want to use it in a typesafe way, so if your Map were hashing from a
String to a CustomerAddress object, you'd actually write:
Map<String,CustomerAddress> myMap =
new ConcurrentHashMap<String,CustomerAddress>();
I did say you have to get used to that <type> notation. Note that in 1.5, the Map interface is
now declared as Map<K, V> (for Keys and Values); the iteration methods are declared in the
interface as Enumeration<K> keys() and Collection<V> values() . So in this example
you would get Enumeration<String> keys() and Collection<CustomerAddress> val-
ues from the “keys” and “values” methods, respectively.
Since you give the type for the keys and values when you instantiate a class implementing
Map , you get back the iterators with the correct types built in, meaning no downcast needed
when you extract the elements.
printf is back
In the early days of Java, it was common for people to try to bring the C-language printf
functionality to Java. Most of these attempts worked only for certain cases, and were not
really object-oriented approaches. After much prodding from developers and much internal
debate, Sun relented and included printf functionality into Java 5.
The functionality is contained in the new java.util.Formatter class (not to be confused
with the existing DateFormat , NumberFormat , etc., classes) and is also available in conveni-
ence routines in System.out (actually, in the PrintStream and PrintWriter classes). The
Search WWH ::




Custom Search