Infolinks In Text Ads

Feb 12, 2011

Map.Entry Interface

Map.Entry Interface -
    Map.Entry Interface is used to hold the view of key-value pair from Map.
Map interface has a method entrySet() which returns the Set object that holds the key value pair. You can iterate the key and value from getKey() and getValue().
You can also set value an element by the setValue() method.

List of methods within the Map.Entry Interface -

 boolean equals(Object o) -
         This method is used to compares the specified object with this entry. If both are equal it return true otherwise return false.

 K getKey() -
    This method returns the key of map.

 V getValue() -
    This method returns the value of corresponding to this entry.

 int hashCode() -
    This method is used to return the hash code value for this map entry.

 V setValue(V value) -
    This method is used to set the values at  corresponding the key.


Example -
public class Test {
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("1","ram");
        map.put("2","two");
       
        Iterator s = map.entrySet().iterator();
        while(s.hasNext()){
            Map.Entry m = (Map.Entry)s.next();
            System.out.println(m.getKey());
            System.out.println(m.getValue());

        }
    }
}

3 comments:

  1. Nice article , just to add the functionality of Enumeration interface is duplicated by the Iterator interface.
    Iterator has a remove() method while Enumeration doesn't. Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects, where as using Iterator we can manipulate the objects also like adding and removing the objects.

    to read more see here difference between iterator and enumeration

    ReplyDelete
  2. hi welcome to this blog. really you have post an informative blog. it will be really helpful to many peoples. thank you for sharing this blog.
    java training in chennai

    ReplyDelete
  3. hi welcome to this blog. really you have post an informative blog. it will be really helpful to many peoples. thank you for sharing this blog.
    java training in chennai

    ReplyDelete

LinkWithin

Related Posts Plugin for WordPress, Blogger...