HashMap- Universe of keys small

  • Thread starter TobiasF
  • Start date
  • Tags
    Universe
In summary, direct addressing is a simple technique that works well for a small universe of keys. However, for a dynamic set with a large universe of keys, using this technique may not be efficient as it can be memory-intensive. This is due to the potential for hash collisions, which may require additional logic to handle.
  • #1
TobiasF
1
0

Homework Statement

[/B]
"Direct addressing is a simple technique that works well when the universe U of
keys is reasonably small. Suppose that an application needs a dynamic set in which
each element has a key drawn from a not too large universe U. We shall assume that no two elements have the same key.
To represent the dynamic set, we use an array, or direct-address table, denoted
by T =[0, ...m-1]. Why can't the size of the universe be very large?"

Homework Equations

The Attempt at a Solution


I am entertaining the possibility that storing a large set of keys might be heavy on memory. However, I am not quite convinced: If we know that we are going to need a lot of keys for an application, then storing a large array is going to be resource-draining anyway.
 
Physics news on Phys.org
  • #2
What is large? are you storing 10, 100, 1000 or 10,000 keys in memory? how big is each key?

There are many database engines that store whole tables in memory for faster access and they are on the order of 10,000 rows or more limited only by available memory.

In general, I use the 10,000 value as a tipping point between in-memory and disk based storage or between using a simple structure like a hash table (or properties object in Java) and switching to a database strategy.

With respect to your key universe not being large, it may have something to do with hash collisions where two elements have the same hash key and so now you have to add additional logic to handle the collision ie use a list object to hold both elements.
 

FAQ: HashMap- Universe of keys small

What is a HashMap?

A HashMap is a data structure in computer science that is used to store and retrieve key-value pairs. It is a part of the Java Collection Framework and allows for efficient retrieval of data by using a hashing function to map a key to its corresponding value.

What does "Universe of keys small" mean in the context of HashMap?

In a HashMap, the "universe of keys small" means that the total number of possible keys that can be used to store data in the HashMap is limited. This is because the hashing function used in HashMap has a finite range of values that it can map keys to.

How does HashMap handle collisions?

Collisions occur in a HashMap when two different keys are mapped to the same index in the underlying array. HashMap handles collisions by using a linked list at each index to store multiple key-value pairs. This ensures that all the key-value pairs are stored and can be retrieved accurately.

What is the time complexity of operations in a HashMap?

The time complexity of operations in a HashMap is O(1) on average. This means that the time taken to retrieve or insert data in a HashMap does not depend on the size of the HashMap, making it a very efficient data structure for storing and retrieving data.

Can a HashMap contain duplicate keys?

No, a HashMap cannot contain duplicate keys. This is because the hashing function used in HashMap maps each key to a unique index in the underlying array. If a duplicate key is inserted, it will overwrite the previous key-value pair at that index.

Similar threads

Replies
2
Views
2K
Replies
32
Views
3K
Replies
4
Views
3K
Replies
14
Views
3K
Replies
8
Views
2K
Back
Top