Java Program to Implement HashMap API

This Java program is to Implement HashMap Collection API. The Map interface provides three collection views, which allow a map’s contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map’s collection views return their elements.

Here is the source code of the Java program to Implement HashMap Collection API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.Collection;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Map;
  5. import java.util.Map.Entry;
  6. import java.util.Set;
  7.  
  8. public class HashMapImpl<K, V>
  9. {
  10.     private HashMap<K, V> hashMap;
  11.  
  12.     /*
  13.      * Constructs an empty HashMap with the default initial capacity (16) and
  14.      * the default load factor (0.75).
  15.      */
  16.     public HashMapImpl()
  17.     {
  18.         hashMap = new HashMap<K, V>();
  19.     }
  20.  
  21.     /*
  22.      * Constructs an empty HashMap with the specified initial capacity and the
  23.      * default load factor (0.75).
  24.      */
  25.     public HashMapImpl(int initialCapacity)
  26.     {
  27.         hashMap = new HashMap<K, V>(initialCapacity);
  28.     }
  29.  
  30.     /*
  31.      * Constructs an empty HashMap with the specified initial capacity and load
  32.      * factor.
  33.      */
  34.     public HashMapImpl(int initialCapacity, float loadFactor)
  35.     {
  36.         hashMap = new HashMap<K, V>(initialCapacity, loadFactor);
  37.     }
  38.  
  39.     /* Constructs a new HashMap with the same mappings as the specified Map. */
  40.     public HashMapImpl(Map<? extends K, ? extends V> m)
  41.     {
  42.         hashMap = new HashMap<K, V>(m);
  43.     }
  44.  
  45.     /* Removes all of the mappings from this map. */
  46.     public void clear()
  47.     {
  48.         hashMap.clear();
  49.     }
  50.  
  51.     /*
  52.      * Returns a shallow copy of this HashMap instance: the keys and values
  53.      * themselves are not cloned.
  54.      */
  55.     public Object clone()
  56.     {
  57.         return hashMap.clone();
  58.     }
  59.  
  60.     /* Returns true if this map contains a mapping for the specified key. */
  61.     public boolean containsKey(Object key)
  62.     {
  63.         return hashMap.containsKey(key);
  64.     }
  65.  
  66.     /* Returns true if this map maps one or more keys to the specified value. */
  67.     public boolean containsValue(Object value)
  68.     {
  69.         return hashMap.containsValue(value);
  70.     }
  71.  
  72.     /* Returns a Set view of the mappings contained in this map. */
  73.     public Set<Map.Entry<K, V>> entrySet()
  74.     {
  75.         return hashMap.entrySet();
  76.     }
  77.  
  78.     /*
  79.      * Returns the value to which the specified key is mapped, or null if this
  80.      * map contains no mapping for the key.
  81.      */
  82.     public V get(Object key)
  83.     {
  84.         return hashMap.get(key);
  85.     }
  86.  
  87.     /* Returns true if this map contains no key-value mappings. */
  88.     public boolean isEmpty()
  89.     {
  90.         return hashMap.isEmpty();
  91.     }
  92.  
  93.     /* Returns a Set view of the keys contained in this map. */
  94.     public Set<K> keySet()
  95.     {
  96.         return hashMap.keySet();
  97.     }
  98.  
  99.     /* Associates the specified value with the specified key in this map. */
  100.     public V put(K key, V value)
  101.     {
  102.         return hashMap.put(key, value);
  103.     }
  104.  
  105.     /* Copies all of the mappings from the specified map to this map. */
  106.     public void putAll(Map<? extends K, ? extends V> m)
  107.     {
  108.         hashMap.putAll(m);
  109.     }
  110.  
  111.     /* Removes the mapping for the specified key from this map if present. */
  112.     public V remove(Object key)
  113.     {
  114.         return hashMap.remove(key);
  115.     }
  116.  
  117.     /* Returns the number of key-value mappings in this map. */
  118.     public int size()
  119.     {
  120.         return hashMap.size();
  121.     }
  122.  
  123.     /* Returns a Collection view of the values contained in this map. */
  124.     public Collection<V> values()
  125.     {
  126.         return hashMap.values();
  127.     }
  128.  
  129.     public static void main(String... arg)
  130.     {
  131.         HashMapImpl<Integer, Integer> hashMap = new HashMapImpl<Integer, Integer>();
  132.  
  133.         hashMap.put(1, 100);
  134.         hashMap.put(2, 200);
  135.         hashMap.put(3, 300);
  136.  
  137.         Map<Integer, Integer> anotherMap = new HashMap<Integer, Integer>();
  138.         anotherMap.put(4, 400);
  139.         anotherMap.put(5, 500);
  140.  
  141.         hashMap.putAll(anotherMap);
  142.  
  143.         System.out.println("the key set of the map is ");
  144.         Set<Integer> keySet = hashMap.keySet();
  145.         Iterator<Integer> itr = keySet.iterator();
  146.         while (itr.hasNext())
  147.         {
  148.             System.out.print(itr.next() + "\t");
  149.         }
  150.         System.out.println();
  151.  
  152.         System.out.println("the values of the map is ");
  153.         Collection<Integer> collectionValues = hashMap.values();
  154.         itr = collectionValues.iterator();
  155.         while (itr.hasNext())
  156.         {
  157.             System.out.print(itr.next() + "\t");
  158.         }
  159.         System.out.println();
  160.  
  161.         System.out.println("the entry set of the map is ");
  162.         Iterator<Entry<Integer, Integer>> eitr;
  163.         Set<Entry<Integer, Integer>> entrySet = hashMap.entrySet();
  164.         eitr = entrySet.iterator();
  165.         while (eitr.hasNext())
  166.         {
  167.             System.out.println(eitr.next() + "\t");
  168.         }
  169.         System.out.println("the hash Map contains Key 3 :" + hashMap.containsKey(3));
  170.         System.out.println("the hash Map contains Value 600 :" + hashMap.containsValue(600));
  171.         System.out.println("the size of the hash Map is " + hashMap.size());
  172.         hashMap.clear();
  173.         if (hashMap.isEmpty())
  174.             System.out.println("the hash Map is empty");
  175.         else
  176.             System.out.println("the hash Map is not empty");
  177.     }
  178. }


$javac HashMapImpl.java
$java HashMapImpl
the key set of the map is 
1	2	3	4	5	
the values of the map is 
100	200	300	400	500	
the entry set of the map is 
1=100	
2=200	
3=300	
4=400	
5=500	
the hash Map contains Key 3 :true
the hash Map contains Value 600 :false
the size of the hash Map is 5
the hash Map is empty

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.