Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of

If you need to handle unique keys paired to values, use

, if the order of the keys is not important.

If you want the pairs sorted on key continuously, use

instead, that's slower, however.

We create a HashMap that we expect not to grow over 50 pairs most of the time,...

if it doesn't exist yet.

We fill it with unique Strings as keys with Integer instances as their associated values .

Search for key "Amsterdam" .

Get the associated value, where we cast the returned Object reference to Integer, because we know for sure, that that's the value's type .

Remove the pair.

Get the keys as a Set .....

to iterate through the Map.

We cast the Object reference from ) to String, because we're positive that String is the only key-type we used.

If the key equals "Moscow", we remove that pair, by asking the Iterator to do so.

This is IMPORTANT : when iterating, use the iterator's method to remove lest you risk disrupting your program's logic.

Show how many pairs are in the Map.

Empty it.




All collections are Serializable, so provided they contain serializable elements, they can easily be serialized to/from disk or a network connection, f.e.




1.5 Generics,...

making these casts unecessary.


If you need to bind multiple values to single keys, you can use any reference as second parameter, for example one pointing to an ArrayList .

Using generics, you would then substitute ArrayList for Integer, or List, if you want to be able to attach different List implementations.


LinkedHashMap maintains the insertion-order of keys, as opposed to a regular HashMap.

A WeakHashMap allows unused keys to be garbage-collected and automatically removed from the map.