Reputation: 13
Hey I'm currently doing a program which uses the already built in hashtables in java. I'm just wondering how do I get certain values that take up the same index in the hashtable if they're dealt with by singly linked lists.
As if the word "meat" and "mate" and "team" take up index number 285, how would I get the word "team" instead of meat, or the other way around?
Thanks
-EDIT
Sorry I meant if I looked up the word say, 'meat', how would I get all the other words in that index as opposed to looking up the index like I said. I just want to return all the values at the index in the hashmap
Upvotes: 0
Views: 116
Reputation: 16
As specified by Kayaman in the above answer,I would also suggest to use Hashmap over hashtable since it is advisable to use hashtable only when you need to perform thread safe operations.
Upvotes: 0
Reputation: 73558
Hashtable
, it's an outdated class (for over 15 years). Use HashMap
instead.Upvotes: 0
Reputation: 533500
If you use Java 8, then HashMap uses a tree for collisions. This means that the worst case time for put/get/remove is O(log N)
instead of O(N)
for a linked list as it was in previous versions.
You can also use ConcurrentHashMap for concurrent access.
As if the word "meat" and "mate" and "team" take up index number 285, how would I get the word "team" instead of meat, or the other way around?
You ask for the key, not the index. It will only get the value if the key matches. If you ask for a key which is not there, but would have a collision, map.get(key)
will return null
.
Upvotes: 1