Hashmaps

Hashmaps

JAVA BASIC BLOCKS-1

Hashmaps store a pair of values i.e. a key and its corresponding value.

The keys are unique while values corresponding to each key can be the same or different.

First, we need to import a package

import java.util.HashMap;

This is how we declare a hashmap. Now let's look into some operations.

INSERTION


import java.util.HashMap;

public class hashmap{
    public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();

        map.put("Reuben",48);
        map.put("Logan",12);
        map.put("Howard",70);
        System.out.println(map);
    }

}

Hashmap doesn't store values in a particular order.

While inserting a new value if the key is already existing then the value is updated.

import java.util.HashMap;

public class hashmapsss {
    public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();

        map.put("Reuben",48);
        map.put("Logan",12);
        map.put("Howard",70);
        System.out.println(map);

        map.put("Howard",72);
        System.out.println(map);
    }

}

TO REMOVE A KEY-VALUE PAIR

import java.util.HashMap;
import java.util.Map;

public class hashmapsss {
    public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();

        map.put("Reuben",48);
        map.put("Logan",12);
        map.put("Howard",70);

        map.remove("Logan");
        System.out.println(map);

    }

}

TO CHECK IF A KEY ALREADY EXISTS


import java.util.HashMap;

public class hashmapsss {
    public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();

        map.put("Reuben",48);
        map.put("Logan",12);
        map.put("Howard",70);

        if(map.containsKey("Logan")){
            System.out.println("KEY PRESENT");
        }
        else{
            System.out.println("KEY ABSENT");
        }

    }

}

TO OBTAIN THE VALUE OF A CORRESPONDING KEY

import java.util.HashMap;

public class hashmap{
    public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();

        map.put("Reuben",48);
        map.put("Logan",12);
        map.put("Howard",70);

        System.out.println(map.get("Reuben"));
        System.out.println(map.get("Sheldon"));

    }

}

Sheldon was not present so it printed null.

ITERATION IN HASHMAP

Syntax

Map.Entry<Integer, Integer> e: Map.entrySet()


import java.util.HashMap;
import java.util.Map;

public class hashmapsss {
    public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();

        map.put("Reuben",48);
        map.put("Logan",12);
        map.put("Howard",70);
        System.out.println(map);

        for(Map.Entry<String,Integer> e: map.entrySet()){
            System.out.println(e.getKey());
            System.out.println(e.getValue());
        }

    }

}


Let's look at a few problems

Count the frequency of each element in the array


import java.util.HashMap;
import java.util.Map;

public class frequencyofeachelement {
    public static void main(String[] args) {
        int[] arr={10,5,10,15,10,5};
        frequency(arr);

    }

    static void frequency(int[] arr){
        int n= arr.length;
        Map<Integer,Integer> map=new HashMap<>();
        for (int i = 0; i <n ; i++) {
            if(map.containsKey(arr[i])){
                map.put(arr[i], map.get(arr[i])+1);
            }
            else {
                map.put(arr[i],1);
            }
        }

        for(Map.Entry<Integer,Integer>e: map.entrySet()){
            System.out.println(e.getKey()+" frequence: "+e.getValue());
        }
    }
}

Find the highest/lowest frequency element

Follows the same approach as the above problem.


import java.util.HashMap;
import java.util.Map;

public class maxfrequencyelement {
    public static void main(String[] args) {
        int[] arr={6, 7, 8, 7, 6,7};
        maxfreq(arr);

    }

    static void maxfreq(int[]arr){
        int n= arr.length;
        Map<Integer,Integer> map=new HashMap<>();

        for (int i = 0; i <n ; i++) {
            if(map.containsKey(arr[i])){
                map.put(arr[i],map.get(arr[i])+1);
            }
            else {
                map.put(arr[i],1);
            }
        }
        int maxFreq = 0, minFreq = n;
        int maxElement = 0, minElement = 0;

        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            int count = entry.getValue();
            int element = entry.getKey();

            if (count > maxFreq) {
                maxElement = element;
                maxFreq = count;
            }
            if (count < minFreq) {
                minElement = element;
                minFreq = count;
            }
        }

        System.out.println("The highest frequency element is: " + maxElement);
        System.out.println("The lowest frequency element is: " + minElement);
    }

}

Did you find this article valuable?

Support Reuben's blog by becoming a sponsor. Any amount is appreciated!