Aside from the
java.util.Collection interface, the Java JDK has the
java.util.Map interface as well. It is sometimes also called an Associated Array or a Dictionary. A map defines key value mappings. Implementations of the Map interface do not contain collections of objects. Instead they contain collections of key->value mappings. It can be thought of as an array where the index doesn't need to be an integer.
|Code section 5.17: Use of a map.
1 import java.util.Map; 2 import java.util.Hashtable; 3 ... 4 Map map = new Hashtable; 5 ... 6 map.put(key, value);
Use the Map interface if you need to keep related objects together in a Map where you can:
In the above example, the same operations are made with two different map implementations:
We see that only the
TreeMap has sorted the keys. Beware of the generics. The Map interface is tricky. The methods
remove are not generic. This means that you must be careful of the type of the key:
remove call has done nothing because
"2" is a
String, not an
Integer so no key and value has been found and removed.
The Map interface has the following implementations:
if (k1==k2). (In normal Map implementations (like HashMap) two keys k1 and k2 are considered equal if and only
if (k1==null ? k2==null : k1.equals(k2)).) Not-synchronized.
The following table lists all the synchronized map classes:
Manage research, learning and skills at defaultLogic. Create an account using LinkedIn or facebook to manage and organize your Digital Marketing and Technology knowledge. defaultLogic works like a shopping cart for information -- helping you to save, discuss and share.Visit defaultLogic's partner sites below: