Java HashMap

HashMap is a class which is available in java.util package. The HashMap is a class of the Java collections framework provides the functionality of hash table data structure. In Java HashMap, elements are stored in key/value pairs. Keys are unique values associated with individual values. HashMap cannot contains duplicate keys.

HashMap Syntax:
                        
                            // hashMap creation with 8 capacity and 0.6 load factor
                            HashMap<K, V> numbers = new HashMap<>();
                            HashMap<String, Integer> numbers = new HashMap<>();
                        
                    
Basic Operations on Java HashMap

The HashMap class provides various methods to perform different operations on hashmaps. We will look at some commonly used operations.

  1. 1. Add elements
  2. 2. Access elements
  3. 3. Change elements
  4. 4. Remove elements
  5. 5. Iterate through a HashMap
1. Add elements to a HashMap

To add a single element to the hashmap, we use the put() method of the HashMap class.

                        
                            import java.util.HashMap;

                            class HashMapExample1 {
                                public static void main(String[] args) {

                                    // create a hashmap
                                    HashMap<String, Integer> numbers = new HashMap<>();

                                    // add elements to hashmap
                                    languages.put("Java", 8);
                                    languages.put("JavaScript", 1);
                                    languages.put("Python", 3);
                                    System.out.println("HashMap: " + languages);
                                }
                            }
                        
                    
2. Access HashMap Elements

We can use the get() method to access the value from the hashmap.

                        
                            import java.util.HashMap;

                            class HashMapExample2 {
                                public static void main(String[] args) {

                                    // create a hashmap
                                    HashMap<String, Integer> numbers = new HashMap<>();

                                    // add elements to hashmap
                                    languages.put("Java", 8);
                                    languages.put("JavaScript", 1);
                                    languages.put("Python", 3);
                                    System.out.println("HashMap: " + languages);

                                    // get() method to get value
                                    String value = languages.get(1);
                                    System.out.println("Value at index 1: " + value);

                                    // return set view of keys
                                    // using keySet()
                                    System.out.println("Keys: " + languages.keySet());

                                    // return set view of values
                                    // using values()
                                    System.out.println("Values: " + languages.values());

                                    // return set view of key/value pairs
                                    // using entrySet()
                                    System.out.println("Key/Value mappings: " + languages.entrySet());
                                }
                            }
                        
                    
3. Change HashMap Elements

We can use the replace() method to change the value associated with a key in a hashmap.

                        
                            import java.util.HashMap;

                            class HashMapExample3 {
                                public static void main(String[] args) {

                                    // create a hashmap
                                    HashMap<String, Integer> numbers = new HashMap<>();

                                    // add elements to hashmap
                                    languages.put("Java", 8);
                                    languages.put("JavaScript", 1);
                                    languages.put("Python", 3);
                                    System.out.println("HashMap: " + languages);

                                    // change element with key 2
                                    languages.replace(2, "C++");
                                    System.out.println("HashMap using replace(): " + languages);
                                }
                            }
                        
                    
4. Remove HashMap Elements

To remove elements from a hashmap, we can use the remove() method.

                        
                            import java.util.HashMap;

                            class HashMapExample4 {
                                public static void main(String[] args) {

                                    // create a hashmap
                                    HashMap<String, Integer> numbers = new HashMap<>();

                                    // add elements to hashmap
                                    languages.put("Java", 8);
                                    languages.put("JavaScript", 1);
                                    languages.put("Python", 3);
                                    System.out.println("HashMap: " + languages);

                                    // remove element associated with key 2
                                    String value = languages.remove(2);
                                    System.out.println("Removed value: " + value);

                                    System.out.println("Updated HashMap: " + languages);
                                }
                            }
                        
                    
5. Iterate through a HashMap

To iterate through each entry of the hashmap, we can use Java for-each loop. We can iterate through keys only, vales only, and key/value mapping.

                        
                            import java.util.HashMap;

                            class HashMapExample4 {
                                public static void main(String[] args) {

                                    // create a hashmap
                                    HashMap<String, Integer> numbers = new HashMap<>();

                                    // add elements to hashmap
                                    languages.put("Java", 8);
                                    languages.put("JavaScript", 1);
                                    languages.put("Python", 3);
                                    System.out.println("HashMap: " + languages);

                                    // iterate through keys only
                                    System.out.print("Keys: ");
                                    for (Integer key : languages.keySet()) {
                                    System.out.print(key);
                                    System.out.print(", ");
                                    }

                                    // iterate through values only
                                    System.out.print("\nValues: ");
                                    for (String value : languages.values()) {
                                    System.out.print(value);
                                    System.out.print(", ");
                                    }
                                    
                                    // iterate through key/value entries
                                    System.out.print("\nEntries: ");
                                    for (Entry entry : languages.entrySet()) {
                                    System.out.print(entry);
                                    System.out.print(", ");
                                    }
                                }
                            }