Map Entry is a nested interface of map implimentation of which is provided by all the map implimenting classes.
This interface provides following methods:-
=> getKey() : is used to obtain reference of key from entry object.
Syntax : public Object getkey();
=> getValue() : is used to otain the reference of the value object from entry object.
Syntax : public Object getValue();
HashMap and TreeMap classes provides implementation of map interface through abstract Map class. The difference between HashMap and TreeMap is in their underline data structure.
HasMap usese HashTable and the TreeMap uses BinarySearch tree for storing elements.
Example: Map.Entry and HashMap
import java.util.*;
class TreeMapTest
{
public static void main(String args[])
{
Map.Entry m;
HashMap map = new HashMap();
map.put("India","New Delhi");
map.put("Pakistan","Islamabad");
map.put("Nepal","Kathmandu");
map.put("Japan","Tokyo");
System.out.println("Totla no's of entries in map are : "+map.size());
Set s = map.entrySet();
Iterator itr = s.iterator();
while(itr.hasNext())
{
m = (Map.Entry)itr.next();
System.out.println(m.getKey()+"t"+m.getValue());
}
System.out.println("Press Ctrl+C to terminate...");
Scanner in = new Scanner(System.in);
System.out.println("Enter country name to find out capital : ");
while(true)
{
String key = in.nextLine();
System.out.println("Captial is : "+map.get(key));
}
}
}
Output:
You have got interesting posts here. Your blog can go viral, you need some initial traffic only.
ReplyDeleteHow to get initial traffic?? Search google for: Ferdeck's tricks