What is Map in Java?
Other than List and Set Map is used to Store Key and Value Pair.The key is unique and values are duplicate allowed. Map implement the collection Interface. It allows only one null value which is stored in the zeroth index.
These are the some of the classes implements the Map<K,V> Interface. HashMap, Hashtable, IdentityHashMap, LinkedHashMap.
Example:
Other than List and Set Map is used to Store Key and Value Pair.The key is unique and values are duplicate allowed. Map implement the collection Interface. It allows only one null value which is stored in the zeroth index.
These are the some of the classes implements the Map<K,V> Interface. HashMap, Hashtable, IdentityHashMap, LinkedHashMap.
Example:
public class HelloWorld {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Hello");
list.add("World");
list.add("Hello1");
list.add("World2");
Map<Integer,String> map = new HashMap<Ineger,String>();
for(int i=0;i<list.size();i++)
{
map.put(i, list.get(i));
}
System.out.println("Data stored in mainlist "+map.get(2));
}
}
No comments:
Post a Comment