Pages

What is HashMap?

What is HashMap?


MAP(I)::   
  • It is not the child interface of collection
  • If we want to represent a group of objects as key-value pairs then we should go for Map
  • Both keys and values are objects only
  • Duplicate key is not allowed but values can be duplicated
  • Each key-value pair is called Entry. Hence map is considered as a collection of entry objects
Entry(I)::
  • A map is a group of key-value pairs and each key-value pair is called an Entry. Hence map is considered as a collection of entry objects.
  • Without existing map object there is no chance of existing entry object. Hence entry interface is defined inside map interface.
interface map{
interface Entry{
Object getKey();
Object getValue()
Object SetValue(Object newobj)
}
}
Map Interface Methods ::
  • Object put(Object key,Object value) :: To add 1 key-value pair to the map 
Note:: If the key is already is present then old value will be replaced with new value and returns old value.
  • void putAll(Map m)
  • Object get (Object key)
  • Object remove(Object key)
  • boolean containsKey(Object key)
  • boolean containsValue(Object value)
  • boolean isEmpty()
  • int size()
  • void clear();
  • Below 3 methods  are collection views of map:
  • Set keySet()
  • Collection values()
  • Set entrySet() 


HashMap

##sample demo program ###
import java.util.*;
public class HashMapDemo {
public static void main(String[] args) {
HashMap<String, String> hmap = new HashMap();
hmap.put("key1", "value1");
hmap.put("key2", "value2");
hmap.put("key3", "value3");
hmap.put("key4", "value4");
System.out.println("keySet()::=" + hmap.keySet() + "\n" + hmap.keySet().getClass());
System.out.println("values()::=" + hmap.values() + "\n" + hmap.values().getClass());
System.out.println("entrySet() ::=" + hmap.entrySet() + "\n" + hmap.entrySet().getClass());

Set<String> set = hmap.keySet();
System.out.println("keys=" + set);
System.out.println("Iterating keys");
// iterate keys
for (String d : set) {
System.out.println(d);
}
System.out.println("--------------");

Collection<String> values = hmap.values();
System.out.println("Values=" + values);
System.out.println("Iterating Values");
System.out.println("Iterating values() using Iterator interface");
Iterator it = values.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("Iterating values() using for-each loop");
for (String va : hmap.values()) {
System.out.println(va);
}
System.out.println("Iterating entries of a hashmap");
for (Map.Entry<String, String> s : hmap.entrySet()) {
System.out.println(s.getKey() + "==" + s.getValue());
}
// Get specific value based on its key
System.out.println("Getting specific value of a key using get() method." + "\n" + "value of key3=" + hmap.get("key3"));
// Get all hashmap values based on its key using get() method
System.out.println("Get all hashmap values based on its key using get() method");
for (Map.Entry<String, String> s : hmap.entrySet()) {
System.out.println(s.getKey() + "==" + hmap.get(s.getKey()));
}
}
}
Output:
Hash Map program in java

Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.