MVantastic
MVantastic

Reputation: 13

Hashtable library in Java

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

Answers (3)

Nikhil Patil
Nikhil Patil

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

Kayaman
Kayaman

Reputation: 73558

  1. Don't use Hashtable, it's an outdated class (for over 15 years). Use HashMap instead.
  2. The public API in either class doesn't provide the functionality you're looking for, so you don't have a nice and clean way to do what you want.
  3. Your possibilities are either ugly reflection hacks, or creating your own implementation that does provide you with the methods to access the internal structure.

Upvotes: 0

Peter Lawrey
Peter Lawrey

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

Related Questions