Java Reference
In-Depth Information
fail with a
ConcurrentModificationException
; you need to use an
Iterator
to control
the loop:
// Version 2: get ALL the keys and values
// with concurrent modification
Iterator
<
String
>
it
=
map
.
keySet
( ).
iterator
( );
while
while
(
it
.
hasNext
( )) {
String key
=
it
.
next
( );
iif
(
key
.
equals
(
"Sun"
)) {
it
.
remove
();
continue
continue
;
}
System
.
out
.
println
(
"Company "
+
key
+
"; "
+
"Address "
+
map
.
get
(
key
));
}
TIP
The
Hashtable
methods are synchronized, for use with multiple threads. They are there-
fore slower, and unless you need the synchronization, use the newer
HashMap
.
Storing Strings in Properties and Preferences
Problem
You need to store keys and values that are both strings, possibly with persistence across runs
of a program—for example, program customization.
Solution
Use a
java.util.prefs.Preferences
object or a
java.util.Properties
object.
Discussion
Here are three approaches to customization based on the user's environment. Java offers
Preferences
and
Properties
for cross-platform customizations.