Map

1) If we want to represent a group of objects as “key-value” pair then we should go for Map interface.
2) Both key and value are objects only.
3) Duplicate keys are not allowed but values can be duplicated
4) Each key-value pair is called “one entry”.

Diagram:
mapeg
Diagram:
mapdia
• Map interface is not child interface of Collection and hence we can’t apply Collection interface methods here.
• Map interface defines the following specific methods.
1) Object put(Object key,Object value);
• To add an entry to theMap, if key is already available then the old value replaced with new value and old value will be returned.
Example:
import java.util.*;
class Map
{
public static void main(String[] args)
{
HashMap m=new HashMap();
m.put(“100″,”vijay”);
System.out.println(m);//{100=vijay}
m.put(“100″,”bhaskar”);
System.out.println(m);//{100=bhaskar}
}
}

2) void putAll(Map m);
3) Object get(Object key);
4) Object remove(Object key);
• It removes the entry associated with specified key and returns the corresponding value.
5) boolean containsKey(Object key);
6) boolean containsValue(Object value);
7) boolean isEmpty();
8) Int size();
9) void clear();
10) Set keySet();
• The set of keys we are getting.
11) Collection values();
• The set of values we are getting.
12) Set entrySet();
• The set of entryset we are getting.

Leave a Reply

Your email address will not be published. Required fields are marked *