Java Notes
Java Notes
Introduction
In Java, a HashMap is a data structure that stores key-value pairs in a
way that allows for efficient lookup, insertion, and deletion of
elements. It is a part of the Java Collections Framework and is widely
used in various applications.
What is a HashMap?
A HashMap is a hash table-based implementation of
the Map interface. It stores key-value pairs in an array, where each
key is unique and maps to a specific value. The keys are used to
identify the values, and the values can be retrieved using their
corresponding keys.
How HashMap Works
Here's a step-by-step explanation of how a HashMap works:
1. Key-Value Pairs: When you add a key-value pair to
a HashMap, the key is hashed using a hash function. The hash
function generates a hash code, which is an integer value that
represents the key.
2. Index Calculation: The hash code is then used to calculate the
index at which the key-value pair should be stored in the
underlying array.
3. Collision Resolution: If two keys hash to the same index, a
collision occurs. To resolve collisions, HashMap uses a
technique called chaining, where each index in the array points
to a linked list of key-value pairs that hash to that index.
4. Lookup: When you retrieve a value from a HashMap using its
key, the key is hashed, and the resulting hash code is used to
calculate the index at which the key-value pair is stored.
The HashMap then traverses the linked list at that index to find
the key-value pair.
HashMap Methods
Here are some common methods used with HashMap:
put(K key, V value): Adds a key-value pair to the HashMap.
get(Object key): Retrieves the value associated with the
specified key.
containsKey(Object key): Returns true if
the HashMap contains the specified key.
containsValue(Object value): Returns true if
the HashMap contains the specified value.
remove(Object key): Removes the key-value pair associated
with the specified key.
clear(): Removes all key-value pairs from the HashMap.
size(): Returns the number of key-value pairs in the HashMap.
HashMap Example
Here's an example of using a HashMap to store student information:
Java
import java.util.HashMap;
public class A {
public static void main(String[] args) {
String roman = "MCMXCIV"; // Example Roman numeral
int result = romanToInt(roman);
System.out.println("The integer value of " + roman + " is: " +
result);
}
return total;
}
}
Output 7
The integer value of MCMXCIV is: 1994
------------------------------------------------------------------------------------
HashMap vs. Other Data Structures
Here's a comparison of HashMap with other data structures:
ArrayList: HashMap is more efficient than ArrayList for
lookup and insertion operations, especially for large datasets.
However, ArrayList is more suitable for sequential access and
modification of elements.
TreeMap: TreeMap is a sorted map implementation that
provides faster lookup and insertion operations
than HashMap for sorted data. However, TreeMap is slower
than HashMap for unsorted data.
HashSet: HashSet is a set implementation that provides faster
lookup and insertion operations than HashMap for unique
elements. However, HashSet does not store values associated
with keys.
Best Practices for Using HashMap
Here are some best practices for using HashMap:
Use meaningful keys: Choose keys that are meaningful and
easy to understand.
Avoid null keys: Avoid using null keys, as they can
cause NullPointerExceptions.
Use efficient hash functions: Use efficient hash functions to
minimize collisions and improve performance.
Monitor performance: Monitor the performance of
your HashMap and adjust its capacity and load factor
accordingly.
------------------------------------------------------------------------------------
PALINDROME example problem:
class A{
// public static void main(String[] args) {
// int x=121;
// int reverse=0;
// int total=x;
// while(total!=0){
// int digit=(int)(total % 10);
// reverse= (reverse * 10) + digit;
// total=total/10;
// }
// if(reverse == x){
// System.out.println("palindrome reverse"+ reverse +"X:"+
x);
// }else{
// System.out.println("not palindrome");
// }
// }
// }
Output: palindrome reverse 121 X: 121