2. HashTables Flashcards
What is a Map in Java?
A Map in Java is a collection that maps keys to values. It is part of the Java Collections Framework and does not allow duplicate keys, but it allows duplicate values.
What are the main interfaces of the Map hierarchy in Java?
The main interfaces of the Map hierarchy in Java are Map, SortedMap, and NavigableMap. Each of these provides different functionalities for storing key-value pairs.
What is the difference between HashMap and TreeMap?
HashMap is an implementation of the Map interface that uses a hash table for storage, providing constant-time performance for basic operations. TreeMap, on the other hand, implements the NavigableMap interface and stores entries in a sorted order based on the natural ordering of the keys or a specified comparator.
What is the purpose of the put() method in a Map?
The put() method is used to add a key-value pair to the Map. If the key already exists, it updates the value associated with that key.
What does the get() method do in a Map?
The get() method retrieves the value associated with a specified key in the Map. If the key is not found, it returns null.
Explain the concept of key uniqueness in Maps.
In a Map, each key must be unique. If a duplicate key is added, the new value will overwrite the existing value associated with that key.
What is the difference between a Map and a Set in Java?
A Map stores key-value pairs, where each key is unique and maps to a specific value. A Set, on the other hand, is a collection that contains no duplicate elements and does not associate any values with its elements.
How does a HashMap handle collisions?
A HashMap handles collisions using a technique called chaining, where each bucket in the hash table contains a linked list of entries. If multiple keys hash to the same bucket, they are stored in the linked list.
What is the initial capacity of a HashMap?
The initial capacity of a HashMap is the number of buckets it uses to store entries. The default initial capacity is 16, and the load factor is 0.75, which determines when to resize the Map.
What is a load factor in the context of HashMap?
The load factor is a measure of how full the HashMap is allowed to get before its capacity is automatically increased. A load factor of 0.75 is commonly used, meaning the Map will resize when it is 75% full.
What is the difference between the keySet() and values() methods in a Map?
The keySet() method returns a Set view of the keys contained in the Map, while the values() method returns a Collection view of the values contained in the Map.
What happens when you call remove() on a Map?
The remove() method removes the key-value pair associated with the specified key from the Map. If the key does not exist, the Map remains unchanged.
What is a LinkedHashMap?
A LinkedHashMap is a HashMap that maintains a linked list of its entries, preserving the order in which they were inserted. This allows for predictable iteration order.
What is the purpose of the entrySet() method in a Map?
The entrySet() method returns a Set view of the mappings contained in the Map, allowing iteration over both keys and values.
True or False: A TreeMap can have null keys.
False. A TreeMap does not allow null keys because it relies on the natural ordering of keys or a comparator.
How can you iterate over a Map in Java?
You can iterate over a Map using a for-each loop with the keySet(), values(), or entrySet() methods. Each method provides a different way to access the keys, values, or both.
What does the containsKey() method do?
The containsKey() method checks if the Map contains a mapping for the specified key. It returns true if the key exists, and false otherwise.
What is the difference between a Map and a ConcurrentMap?
A ConcurrentMap is a specialized Map designed for concurrent access by multiple threads. It provides additional methods for atomic operations and ensures thread safety.
Explain the role of the comparator in a TreeMap.
In a TreeMap, a comparator defines the order of the keys. If no comparator is provided, the keys must implement the Comparable interface to determine their natural order.
What is a default method in the context of the Map interface?
A default method in the Map interface is a method that has a default implementation, which can be overridden by implementing classes. This feature was introduced in Java 8.
What is the purpose of the computeIfAbsent() method in a Map?
The computeIfAbsent() method computes a value for a specified key if it is not already present in the Map, allowing for lazy initialization.
What is the difference between the replace() and put() methods?
The replace() method updates the value for a specified key only if the key is already mapped to a value, while the put() method adds a new key-value pair or updates an existing one regardless of its current state.
Explain the concept of immutability in relation to Maps.
Immutability in the context of Maps refers to the property that once a Map is created, its key-value pairs cannot be modified. Immutable Maps are useful for ensuring that data remains constant throughout the application.
What is the significance of the Map interface in the Java Collections Framework?
The Map interface is significant in the Java Collections Framework because it provides a standard way to represent key-value pairs, allowing developers to use different implementations like HashMap, TreeMap, or LinkedHashMap interchangeably.