Thursday, August 19, 2010

Maps

What is Map : It is a data type that keeps associations between keys and values , every key in map has a unique value, but value can be associated to several keys. Same as set implementation the Java Library has two implementation for maps a) Hash Map and b) Tree Map.

The most important thing here to remember is both Hash Map & Tree Map implement the Map Interface
 Map <String, Color> favoriteColors = new HashMap<String, Color>();
We can use put method to add association and remove method to remove association, you can also change value of  an existing association simply by calling put again. The get method returns the value associated with a key. If you ask for key that's not associated with any values, then the get method returns null.To find all keys and values in a map, iterate through the key set and find the values that correspond to the keys through get method. Sometimes when you want to enumerate all keys in a map KeySet method yields the set of keys

Check yourself - 1) What is difference between set and map
                          2) Why is the collections of the keys of a map a set

No comments:

Post a Comment