top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

MAP

Hashmap and Hashtable


When we want to represent a group of objects which has a key and value pair we choose Map interface. Example: EId and Ename. Combination of key and value is called one dataset/entry. Each key is an object and each value is an object. In Map, duplicate keys are not allowed but duplicate values are allowed. If you try to add a duplicate key with another value, the old value will be replaced by a new value. Map is an interface which is implemented by Hashmap and Hashtable


HashMap - scope - when there is more number of search operations

  1. Underlying data structure is hashTable

  2. Insertion order is not preserved

  3. Duplicate keys are not allowed, duplicate values are allowed

  4. Null Key is allowed only once

  5. Null values can be multiple


Methods available in HashMap:


  1. Create hashmap object m

HashMap m = new HashMap();

m.put(key,value); => adds one key value pair in the hash map

m.putAll(map m1) => adds entries from m1 map to new map

m.get(key) => returns the value based on the key

m.remove(key) => remove the value along with key

m.containsKey(key) => if key is available in hashmap returns true, else false

m.containsValue(value)=>returns true if value is present in hashmap

m.isEmpty() =verifies if map is empty or not

m.size() => returns no of key value pairs

m.clear();


  1. Other methods wrt entries in hashmap

m.keyset() => retrieve all keys from the hashmap. Return type will be set since there are no duplicates. 

m.values() => retrieve all values from the hashmap. Return type will be collections since there are duplicates. 

m.entryset()=>returns all the entries from the hashmap(key and value)


Map - collection of entries. Entry - combination of key and value and can be represented by special interface called Entry Interface. Subset interface of hashmap class. 

Methods in entry interface. After extracting the single entry.

  1. e.getKey() => returns key 101

  2. e.getValue() => returns value

  3. e.setValue(object y) => updates or replaces the value




 Real time examples:


1.suppose in your project you got a requirement like you have to sort the employee based on seniority and show the result ,now you have already list of employee with you,sort that list based on age property of employee ,in this case use collections.sort().in this case you are avoiding making any DB call.so this will give you faster result.


2.map collection being used to create in memory cache.suppose in your project you got requirement like first 100 records need show based on the default selection whenever user login in the system.In this case use map to store the 100 records in cache when your application gets up.with this you will give user good experience about usage of your application.



Collections are useful in those situations where you doesn't know how much data you have to handled, that means size of data not defined. Like there are so many scenario when data dynamically comes from UI or application ( no fixed size defined) then you must use collections to manage and perform diff operations.


In real time some times we can't estimate the size of the data and type of the data it's better to maintain data as objects through collections.through arrays we can't manage the data because arrays are not auto-grow-able in size.


through collections we can use our required data structures directly. we have different classes for that. in each we have predefined methods to manipulate data in that collection.


Collections are performance wise slow. even though collections provide more features to maintain huge data easily.


Along with the collections classes there are several other classes and interfaces which are part of Collection Framework used to manipulated the collection data.


Below I’m listing the scenarios where we need to use collection classes.


1.Fetch data from database and store each record into suitable collection class.

2.Hold the data and transfer the data from UI to database and vice versa.

3.Sorting the entity objects for example sorting of Employee object based on the Emp_Id or Emp_Name etc. (Assume 4.Employee class have properties Emp_Id and Emp_Name.)

5.Removing of the duplicate data (String or Objects) by using sets.

6.Search the data in the collections class

7.Storing the objects in key value pair.


Hashtable:


Unlike HashMap, Hashtable is a synchronized Map and it is thread-safe which means it can be shared between multiple threads. In Hashtable, you specify an object that can be used as a key and the value that goes with the key. A Hashtable maps keys to values with the help of a hash function. Java provides this function in the form of Object’s hashcode( ) method, which classes override to provide appropriate hash codes. Unlike HashMap, Hashtable does not support null values and null keys because there is null check in the put method implementation of Hashtable.



Difference between HashMap and Hashtable:



  1. Basics of HashMap Vs. Hashtable


Both are hash-based collections in Java used to store data in key/value pairs. HashMap is a Map implementation based on a hash table which provides constant-time performance for inserting and locating pairs. Performance can be adjusted with the use of constructors that allow you to set the capacity and load factor of the hash table. The basic Hashtable is quite similar to the HashMap, even down the method names. It stores key/value pair in hash table. In Hashtable, you specify an object that can be used as a key and the value that goes with the key.


  1. Synchronization of HashMap Vs. Hashtable


Both HashMap and Hashtable use hashing techniques to store values based on the key. Like HashMap, Hashtable uses key/value pairs to store values in a hash table. However, the key difference between the two is synchronization. HashMap is an unsynchronized Map whereas Hashtable is a synchronized Map. This means HashMap is not thread-safe and cannot be shared between multiple threads without proper synchronization code. On the contrary, Hashtable is thread-safe and can be shared between multiple threads. Hashtable is faster than using a HashMap in a synchronized wrapper, if you need to use a synchronized Map.


  1. Null Keys and Null Values for HashMap Vs. Hashtable


The HashMap class provides a map implementation that is based on a Hashtable data structure. This implementation supports all Map operations and allows multiple null values but only one null key so that it could maintain unique key properties. However, it makes no guarantees on the order in which the entries are stored. The Hashtable, on the other hand, maps keys to values with the help of a hash function. Unlike HashMap, Hashtable does not support null values and null keys because there is null check in the put method implementation of Hashtable.


  1. Performance of HashMap Vs. Hashtable


Because HashMap is not a synchronized Map it is much faster and better than a Hashtable in terms of performance, and in fact, uses less memory than Hashtable. Although they are virtually identical, Hashtable is a bit slower than a HashMap but faster than a synchronized HashMap. Inherently, it is not safe to use Hashtable with multithreaded access because only the methods are synchronized. Hashtable is the synchronized counterpart to HashMap. Unsynchronized objects perform better when compared to synchronized objects just like a Hashtable performs better in a single threaded environment.



16 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page