![]() Thus we should exercise care to carefully choose these two factors to design an efficient hashMap. Similarly, when capacity is low, the load factor will be high as we will need to rehash frequently. Note that if the capacity is high, the load factor will be small as no rehashing will be required. (ii) LoadFactor: LoadFactor is the parameter that measures when rehashing – increasing the capacity, will be done. The capacity of the HashMap is always multiplied by 2. Initial Capacity is defined as the capacity of the HashMap object when it is created. (i) Initial Capacity: Capacity is defined as the number of buckets in the HashMap. The performance of HashMap is influenced by two parameters: Each bucket may not have the same capacity and it can have more than one node as well. An array of these nodes is called Bucket. Given below is a structure of a node of HashMap that is programmatically represented as a class.Īs seen from the node representation above, a node has a structure similar to a linked list node. HashMap makes use of array and LinkedList for storing key-value pairs. Internally, the HashMap is an array of nodes. We know that HashMap is a collection of key-value pairs and it makes use of a technique called ‘Hashing’. put some initial values into it using put method The below program shows the initialization of HashMap in Java. We can initialize the HashMap using the put method by putting some values in the map. Once the HashMap is created, we need to initialize it with values. Then in the next statement, we create a HashMap named ‘cities_map’ with key type as Integer and Values as String. The above statement first includes the HashMap class in Java. V=> type of values mapped to the keys in the map Create A HashMapĪ HashMap in Java can be created as follows: import Here, K=> type of keys present in the map The general declaration of HashMap class is: public class HashMap extends AbstractMap Hence, if we need to use HashMap in our code, we first need to import the implementation class using one of the following statements: import java.util.*
0 Comments
Leave a Reply. |