HashMap implements the Map interface and extends the Abstractmap class.
It contains unique elements.
Hashmap contains the value based on the key. It stores values as key value pairs.
It supports null as key.
It maintains no order.
It supports multiple null values.
Following is the hierarchy of hashMap
Map
| implements
|
Abstractmap
| extends
|
HashMap
Example:
/**
* if key is different and content is same for three values then it will
* print all values as shown below example HashMap allows null as key
* values(Using EntrySet Method)
*/
Map map = new HashMap();
map.put("one", "krishna");
map.put("one1", "krishna");
map.put("two", "munna");
map.put("three", "balu");
map.put("four", "tiger");
map.put(null, null);
map.put(null, null);
map.put(null, "null key3");
Set set = map.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry object = (Map.Entry) iterator.next();
System.out.println(object.getKey() + "*******" + object.getValue());
}
// ==============================================
/**
* if key is different and content is same for three values then it will
* print all values as shown below example HashMap allows null as key
* values(Using KeySet Method)
*/
Map map2 = new HashMap();
map2.put("one", "krishna");
map2.put("one1", "krishna");
map2.put("two", "munna");
map2.put("three", "balu");
map2.put("four", "tiger");
map2.put(null, null);
map2.put(null, null);
map2.put(null, "null key3");
Set set2 = map2.keySet();
Iterator iterator2 = set2.iterator();
while (iterator2.hasNext()) {
Object object = (Object) iterator2.next();
System.out.println(object + "***#############****"
+ map2.get(object));
}
// =============================================
/**
* if key is different and content is same for three values then it will
* print all values as shown below example. HashMap allows null as key
* values(Using KeySet Method).Hash Map supports different types of
* objects like strings,integers as values and as well as keys.
*/
Map map42 = new HashMap();
map42.put("one", "krishna");
map42.put("one1", "krishna");
map42.put("two", "munna");
map42.put("three", "balu");
map42.put("four", "tiger");
map42.put(null, null);
map42.put(null, null);
map42.put(null, "null key3");
map42.put(new Integer(54), "null key3");// /integer as key
map42.put("integer", new Integer(88));// integer as value
Set set42 = map42.keySet();
Iterator iterator42 = set42.iterator();
while (iterator42.hasNext()) {
Object object = (Object) iterator42.next();
System.out.println(objec+ "****&&&&&&&&&&&&&&&&"+ map42.get(object));
}
It contains unique elements.
Hashmap contains the value based on the key. It stores values as key value pairs.
It supports null as key.
It maintains no order.
It supports multiple null values.
Following is the hierarchy of hashMap
Map
| implements
|
Abstractmap
| extends
|
HashMap
Example:
/**
* if key is different and content is same for three values then it will
* print all values as shown below example HashMap allows null as key
* values(Using EntrySet Method)
*/
Map map = new HashMap();
map.put("one", "krishna");
map.put("one1", "krishna");
map.put("two", "munna");
map.put("three", "balu");
map.put("four", "tiger");
map.put(null, null);
map.put(null, null);
map.put(null, "null key3");
Set set = map.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry object = (Map.Entry) iterator.next();
System.out.println(object.getKey() + "*******" + object.getValue());
}
// ==============================================
/**
* if key is different and content is same for three values then it will
* print all values as shown below example HashMap allows null as key
* values(Using KeySet Method)
*/
Map map2 = new HashMap();
map2.put("one", "krishna");
map2.put("one1", "krishna");
map2.put("two", "munna");
map2.put("three", "balu");
map2.put("four", "tiger");
map2.put(null, null);
map2.put(null, null);
map2.put(null, "null key3");
Set set2 = map2.keySet();
Iterator iterator2 = set2.iterator();
while (iterator2.hasNext()) {
Object object = (Object) iterator2.next();
System.out.println(object + "***#############****"
+ map2.get(object));
}
// =============================================
/**
* if key is different and content is same for three values then it will
* print all values as shown below example. HashMap allows null as key
* values(Using KeySet Method).Hash Map supports different types of
* objects like strings,integers as values and as well as keys.
*/
Map map42 = new HashMap();
map42.put("one", "krishna");
map42.put("one1", "krishna");
map42.put("two", "munna");
map42.put("three", "balu");
map42.put("four", "tiger");
map42.put(null, null);
map42.put(null, null);
map42.put(null, "null key3");
map42.put(new Integer(54), "null key3");// /integer as key
map42.put("integer", new Integer(88));// integer as value
Set set42 = map42.keySet();
Iterator iterator42 = set42.iterator();
while (iterator42.hasNext()) {
Object object = (Object) iterator42.next();
System.out.println(objec+ "****&&&&&&&&&&&&&&&&"+ map42.get(object));
}
No comments:
Post a Comment