In Java SE 22, what type of values does the HashMap allow?
Answer:
In Java SE 22, the `HashMap` class allows you to store key-value pairs, where:
Keys can be any non-null object (primitive types must be boxed, e.g., Integer, Double, etc.)
Values can be any object type, including null.
Value Types Allowed in `HashMap`:
Objects of any reference type (e.g., String, Integer, List<String>, custom classes)
null values are allowed
Primitive types are not directly allowed — they must be wrapped (e.g., use int as Integer, boolean as Boolean)
Example:
Map <String, String> map = new HashMap<>();
map.put("name", "Alice");
map.put("city", null); // null values are allowed
Important Notes:
Keys must be unique.
Keys can be null (but only onenull key is allowed).
HashMap is not synchronized — use Collections.synchronizedMap() or ConcurrentHashMap for thread-safe use.
Hash Map Values in Java
The HashMap allows one null key and multiple null values in a collection.
What does it mean when we say Vector and Hashtable are synchronized?
Answer:
Vector is the synchronized counterpart of the ArrayList.
Hashtable is the synchronized counterpart to HashMap. This means that the key methods of the class are synchronized.
Vector implements the List interface
What are the differences between a HashMap and a Hashtable in Java?
Answer: Main differences between a HashMap and a Hashtable are:
Synchronization: HashMap is not a synchronized collection. If it is used in multi-thread environment, it may not provide thread safety. A Hashtable is a synchronized collection. Not more than one thread can access a Hashtable at a given moment of time. The thread that works on Hashtable acquires a lock on it and it makes other threads wait till its work is completed.
Null values: A HashMap allows only one null key and any number of null values. A Hashtable does not allow null keys and null values.
Ordering: A HashMap implementation by LinkedHashMap maintains the insertion order of elements. A TreeMap sorts the mappings based on the ascending order of keys. On the other hand, a Hashtable does not provide guarantee of any kind of order of elements. It does not maintain the mappings of key values in any specific order.
Legacy: Hashtable was not the initial part of collction framework in Java. It has been made a collection framework member,
after being retrofitted to implement the Map interface. A HashMap implements Map interface and is a part of collection framework since the beginning.
Iterator: The Iterator of HashMap is a fail-fast and it throws ConcurrentModificationException if any other Thread modifies the map by inserting or removing any element except iterator's own remove() method.
What is one difference between HashMap and Hashtable?
Answer: A Hashtable does not let you have anything that is null.
What is characteristic of the LinkedHashMap?
Answer: The LinkedHashMap collection maintains insertino order ( or, optionally, access order)
What is a TreeMap?
Answer: A TreeMap is a sorted Map.
How can a custom order be defined for TreeSet and TreeMap?
Answer: By using Comparable or Comparator.
What is the purpose of the Queue interface?
Answer: A Queue is designed to hold a listed of tasks that need to be completed.
The LinkedList class has been enhanced to implement the Queue interface.
Answer: Basic Queues can be handled with a LinkedList.
What is the purpose of a PriorityQueue?
Does a PriorityQueue in Java sort elements based on their priorities?
Answer:
Yes, a `PriorityQueue` in Java sorts elements based on their priorities, not their insertion order.
Key Characteristics:
By default, it uses natural ordering (e.g., ascending for numbers, alphabetical for strings).
You can customize the priority by providing a Comparator.
It is a min-heap by default — the smallest element (based on priority) is at the head.
It does not allow null elements.
It is not thread-safe — use PriorityBlockingQueue for concurrent use.