HashMap in Java with Example - Javastudypoint

Friday, March 22, 2019

HashMap in Java with Example

HashMap in Java is a class which is a part of Java Map. The Java HashMap class extends AbstractMap and implements the Java Map interface. It uses a hash table to store a map. It uses a key/values pair to store the values in a map. HashMap uses a technique called Hashing to convert a large string into a small string that represents the same string, it helps in indexing and faster searching.


HashMap in Java



Points to remember about Java hashMap:

1. It is the first implementation class of the Map interface.
2. It is present in java.util package.
3. It uses a hash table to store a map.
4. insertion order is not preserved and it is based on hashcode of keys.
5. duplicate keys are not allowed but values are allowed.
6. null is allowed for key only once but for values multiple times.
7. implements serializable, clonable interface but not RandomAccess interface.

Constructors in Java HashMap:

1. HashMap(): The first constructor creates a default hash map with initial capacity 16 and load factor is 0.75.
2. HashMap(int initial capacity): The second constructor creates a hash map with specified initial capacity and load factor.
3. HashMap(int initial capacity, float loadFactor): The third constructor creates a hash map with specified initial capacity and load factor.
4. HashMap(Map map): The fourth constructor initializes the hash map by using the elements of the map.


Methods of Java HashMap:

HashMap class implements Map and extends the AbstractMap. It does not add any of its own methods.
1. void clear(): Removes all the key/values pair in the map.
2. boolean containsKey(Object key): It returns true if the specified key found in the map.
3. boolean containsValue(Object value): It returns true if the specified value found in the map.
4. Object put(Object key, Object value): It is used to put an entry in the map.
5. Object get(Object key): returns the value associated with the key.
6. Object remove(Object key): It removes the key/values pair in the invoking map.
7. int size(): It returns the number of key/values pair in the invoking map.
8. boolean isEmpty(): It returns true if the invoking map is empty else returns false.
9. Object clone(): It returns a shallow copy of the hash map.
10. Set keySet(): It returns a set that contains the keys in the invoking map.
11. void putAll(Map m): It is used to copy all the elements of a map to another specified map.
12. Collection values(): This method is used to returns a collection values of a hash map.
13. Set entrySet(): It returns a set that contains the entries in the map.

Java HashMap Example


import java.util.*;
class HashMapExample{
 public static void main(String args[]){
  
  //creating a hash map
  HashMap<Integer, String> hmap = new HashMap<Integer, String>();
  
  //adding values in this map
  hmap.put(101, "Rahul");
  hmap.put(107, "Amit");
  hmap.put(103, "Vijay");
  hmap.put(105, "Suresh");
  hmap.put(102, "John");
  hmap.put(106, "Prashant");

  //initially print hash map entries
  System.out.println("Hash Map entries are: "+hmap);
  
  //returns true if the specified key found in the map.
  System.out.println("contain (105) key or not: "+hmap.containsKey(105));
  
  //returns true if the specified value found in the map.
  System.out.println("contain specified value (Suresh) or not: "+hmap.containsValue("Suresh"));
  
  // returns the value associated with the key.
  System.out.println("return value of specified key: "+hmap.get(106));
  
  //returns the number of key/values pair
  System.out.println("Hash Map size is: "+hmap.size());
  
  //returns true if the invoking map is empty 
  System.out.println("check hash map is empty or not: "+hmap.isEmpty());
  
  //returns a set that contains the keys 
  System.out.println("Set that contains keys: "+hmap.keySet());
  
  //returns a set that contains the entries.
  Set s = hmap.entrySet();
  System.out.println("Set that contain entries: "+s);
  
  //get entry object one by one using iterator
  Iterator itr = s.iterator();
  while(itr.hasNext()){
   Map.Entry m = (Map.Entry)itr.next();
   System.out.println("Key: "+m.getKey()+"   "+"Value: "+m.getValue());
  }
 }
}

Output
HashMap in Java with Example


You may also enjoy these related posts:

SortedMap in Java with Example
How to use ArrayDeque in Java with Example?
Queue in Java with Example
Comparable vs Comparator in Java with Example
TreeSet in Java with Example
NavigableSet in Java with Example
How to implement a stack data structure in Java with Example
How to implement a LinkedList in Java with Example
How to add or remove an element in the ArrayList?

No comments:

Post a Comment