The data structure used by a HashMap to store elements is a hash table.

Here's why:

  • Hashing: HashMaps use a hash function to calculate a unique hash code for each key. This hash code determines the index where the key-value pair will be stored in the hash table.
  • Collision Handling: Hash tables handle collisions (when two keys have the same hash code) using techniques like chaining or open addressing.

Therefore, the correct answer is B. Hash table.

HashMap Data Structure: How Elements Are Stored