AppDividend
Latest Code Tutorials

Hashing In Java Example | Java Hashing Tutorial

0

Hashing In Java Example | Java Hashing Tutorial is today’s topic. Hashing is a technique that is used for mapping values to the key, which in turn makes easy to retrieve values by just entering the key. The main advantage of using HASHING in java is that it reduces the time complexity of any program and allows the execution time of essential operation to remain constant even for the more significant side given. But the main problem of the hashing function is that it leads to the collision as two or more keys can point to the same values. If we want to avoid this chain, hashing is mainly used. So, to insert a value in a hash table, the main requirement is a hash index which is calculated using the formula.

Hashing In Java Example

In hashing, there is the hash function that maps keys to some values.

But these hashing function may lead to a collision that is two or more keys are mapped to the same value.

Chain hashing avoids the collision.

An idea is to make each cell of hashtable point to the LinkedList of records that have the same hash function value.

Hashindex: key%number_of_items

Let’s implement hashing in the following ways using Java.

Implementing hashing using a hashtable

See the following code.

import java.util.*;

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

        Hashtable<Integer, String> hm = new Hashtable<Integer, String>();

        hm.put(1, "App");
        hm.put(12, "Dividend");
        hm.put(15, "Best place to learn");
        hm.put(3, "Java");

        System.out.println(hm);
    }
}

See the output.

 

Hashing In Java Example

The above method used is the synchronized way of hashing.

Implementing hashing using a hashmap

See the following code.

import java.util.*;

public class Hashing2 {
    static void createHashMap(int arr[]) {
        HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>();
        for (int i = 0; i < arr.length; i++) {
            Integer c = hmap.get(arr[i]);
            if (hmap.get(arr[i]) == null) {
                hmap.put(arr[i], 1);
            } else {
                hmap.put(arr[i], ++c);
            }
        }
        System.out.println(hmap);
    }

    public static void main(String[] args) {
        int arr[] = { 11, 35, 6, 11, 4, 6, 12 };
        createHashMap(arr);
    }
}

See the output.

 

Implementing hashing using a hashmap

The above method used is the non-synchronized faster implementation of hashing.

Here, we have created hashmap out of array by taking the elements as keys and values as frequencies.

Implementing hashing using LinkedHashMap

See the following program.

import java.util.*;

public class BasicLinkedHashMap {
    public static void main(String a[]) {
        LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>();
        lhm.put("one", "AppDividend.Com");
        lhm.put("two", "https://appdividend.com/amp/category/java-tutorials/");
        System.out.println("Value for key 'one' is: " + lhm.get("one"));
        System.out.println("Size of the map: " + lhm.size());
        System.out.println("Is map empty? " + lhm.isEmpty());
        System.out.println("Contains key 'two'? " + lhm.containsKey("two"));
        System.out.println("delete element 'one': " + lhm.remove("one"));
        System.out.println(lhm);
    }
}

See the output.

 

Implementing hashing using LinkedHashMap

The above method is similar to HashMap, but the only advantage out of it is that it keeps the order of elements.

Implementing hashing using ConcurrentHashMap

See the following program.

import java.util.concurrent.*;

public class ConcurrentHashMapDemo {
    public static void main(String[] args) {
        ConcurrentHashMap<Integer, String> m = new ConcurrentHashMap<Integer, String>();
        m.put(100, "C++");
        m.put(101, "Is");
        m.put(102, "Always");
        m.put(103, "Best");
        System.out.println("ConcurentHashMap: " + m);
        m.putIfAbsent(101, "C++");
        System.out.println("\nConcurentHashMap: " + m);
        m.remove(102, "Always");
        System.out.println("\nConcurentHashMap: " + m);
        m.replace(100, "C++", "Java");
        System.out.println("\nConcurentHashMap: " + m);
    }
}

See the following output.

 

Implementing hashing using ConcurrentHashMap

The above method is similar to the hashtable. The difference is that it is synchronized and faster as multiple locks are used.

Implementing hashing using HashSet

See the following program.

import java.util.*;

public class Test {
    public static void main(String[] args) {
        HashSet<String> h = new HashSet<String>();
        h.add("India");
        h.add("Australia");
        h.add("South Africa");
        h.add("India");
        System.out.println(h);
        System.out.println("Does HashSet contains India or not:" + h.contains("India"));
        h.remove("Australia");
        System.out.println("List after removing Australia:" + h);
        System.out.println("Traversing the list:");
        Iterator<String> i = h.iterator();
        while (i.hasNext())
            System.out.println(i.next());
    }
}

See the output.

 

Implementing hashing using HashSet

The above method used is similar to HashMap. The only difference is that HashSet maintains only keys, not pairs.

Implementing hashing using LinkedHashSet

See the following program.

import java.util.LinkedHashSet;

public class Demo {
    public static void main(String[] args) {
        LinkedHashSet<String> linkedset = new LinkedHashSet<String>();

        linkedset.add("A");
        linkedset.add("B");
        linkedset.add("C");
        linkedset.add("D");

        // This will not add new element as A already exists
        linkedset.add("A");
        linkedset.add("E");

        System.out.println("Size of LinkedHashSet = " + linkedset.size());
        System.out.println("Original LinkedHashSet:" + linkedset);
        linkedset.remove("D");
        System.out.println("Trying to Remove Z which is not " + "present: " + linkedset.remove("Z"));
        System.out.println("Checking if A is present=" + linkedset.contains("A"));
        System.out.println("Updated LinkedHashSet: " + linkedset);
    }
}

See the output.

 

Implementing hashing using LinkedHashSet

The above method is similar to LinkedHashMap. The difference is that it maintains only keys, not pair.

Finally, Hashing In Java Example | Java Hashing Tutorial is over.

Recommended Posts

Java.lang.Boolean Class In Java Example

Deque Interface In Java Example

What are Literals in Java

Java Math Class Tutorial

Dictionary Class In Java Tutorial

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.