Maps are key-value data structures that allow associating keys with values. The Map interface is part of the Java Collections Framework. Common map implementations include HashMap (stores key-value pairs in a hash table), LinkedHashMap (maintains insertion order), TreeMap (stores keys in ascending sorted order), and Hashtable (older synchronized map implementation). Hashing functions calculate hash codes from keys to determine bucket locations, and collisions are resolved through techniques like separate chaining or open addressing.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
8 views
Ali Hassan
Maps are key-value data structures that allow associating keys with values. The Map interface is part of the Java Collections Framework. Common map implementations include HashMap (stores key-value pairs in a hash table), LinkedHashMap (maintains insertion order), TreeMap (stores keys in ascending sorted order), and Hashtable (older synchronized map implementation). Hashing functions calculate hash codes from keys to determine bucket locations, and collisions are resolved through techniques like separate chaining or open addressing.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15
Map
Ali Hassan Map • Maps are key-value data structures that allow you to associate keys with values, and they are part of the Java Collections Framework. • Map is interface Map
Sorted Map
Hash Table Hash Map Navigable Map
Linked Hash Map Tree Map
Hashing Function • A hash function is a mathematical function that takes an input (or "key") and produces a fixed-size string of characters, which is typically a numerical value called a hash code or hash value. The purpose of a hash function is to transform the input data into a fixed-size value, such as an integer, in a way that appears random, and where even a small change in the input results in a significantly different hash code. Calculating the Hash Code • When you insert a key-value pair into a data structure that uses hashing (e.g., a HashMap in Java), the key is passed through the hash function. • The hash function calculates the hash code based on the key's content. The function's implementation depends on the specific algorithm used for hashing. • The hash code is typically an integer value but can be any fixed-size representation. Handling Collisions • Hash functions strive to distribute keys uniformly across a range of hash codes. However, it's possible for different keys to produce the same hash code; this is known as a hash code collision. • When multiple keys produce the same hash code, they are mapped to the same bucket or slot within the data structure. This creates a situation where multiple key-value pairs may be stored in the same bucket. Resolution of Collisions • To resolve collisions, most hash table implementations use a method like separate chaining or open addressing. • Separate Chaining: In this method, each bucket maintains a data structure like a linked list, where multiple key-value pairs with the same hash code are stored. When you need to retrieve a value associated with a key, you search within the linked list in that bucket. • Open Addressing: In this method, when a collision occurs, the algorithm searches for the next available slot within the hash table (e.g., by probing linearly or quadratically) until it finds an empty slot. HashMap • Java HashMap class implements the Map interface which allows us to store key and value pair, where keys should be unique. If you try to insert the duplicate key, it will replace the element of the corresponding key. It is easy to perform operations using the key index like updation, deletion, etc. HashMap class is found in the java.util package. Point To Remember • Java HashMap contains values based on the key. • Java HashMap contains only unique keys. • Java HashMap may have one null key and multiple null values. • Java HashMap is non synchronized. • Java HashMap maintains no order. • The initial default capacity of Java HashMap class is 16 with a load factor of 0.75. Linked Hash Map • Java LinkedHashMap class is Hashtable and Linked list implementation of the Map interface, with predictable iteration order. It inherits HashMap class and implements the Map interface. Point To Remember • Java LinkedHashMap contains values based on the key. • Java LinkedHashMap contains unique elements. • Java LinkedHashMap may have one null key and multiple null values. • Java LinkedHashMap is non synchronized. • Java LinkedHashMap maintains insertion order. • The initial default capacity of Java HashMap class is 16 with a load factor of 0.75. Tree Map • Java TreeMap class is a red-black tree based implementation. It provides an efficient means of storing key-value pairs in sorted order. Point To Remember • Java TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class. • Java TreeMap contains only unique elements. • Java TreeMap cannot have a null key but can have multiple null values. • Java TreeMap is non synchronized. • Java TreeMap maintains ascending order. Hash Table • Java Hashtable class implements a hashtable, which maps keys to values. It inherits Dictionary class and implements the Map interface. • HashTable is an older map implementation in Java. It is synchronized, which means it's thread-safe, but it is less flexible and efficient compared to HashMap. Point To Remember • A Hashtable is an array of a list. Each list is known as a bucket. The position of the bucket is identified by calling the hashcode() method. A Hashtable contains values based on the key. • Java Hashtable class contains unique elements. • Java Hashtable class doesn't allow null key or value. • Java Hashtable class is synchronized. • The initial default capacity of Hashtable class is 11 whereas loadFactor is 0.75.