Java hash table with separate chain collision resolution?

I created a program using the built in java.util.hashtable, but now I need to resolve conflicts using a separate chain. Is it possible with this implementation of a hash table? Is there one out there that uses a separate chain?

+2


a source to share


1 answer


Looking at the source of the implementation of Hashtable, it looks like it already uses a separate chaining. If you look at the class Entry<K,V>

starting at line 901, you will see that it has a link to another entry named next

. If you then look at the method put()

, on line 420 the link next

will be populated via the constructor to be whatever was previously stored in that bucket.

Note that you don't have to worry about such implementation details at all. The Java Collections Framework is probably one of the most widely used frameworks in Java, and as such, you have to assume that the authors tweaked the performance as well as they did.



Another thing I would like to point out is that the Hashtable class is basically replaced with a class HashMap

(which also uses a separate chaining, see here ). The main difference between the two is that all methods from are Hashtable

synchronized, and from HashMap

are not. This leads to better performance in situations where you are working in the same streaming environment (perhaps the reason behind this question?).

If you need a streamlined implementation, then you should consider wrapping the normal HashMap

when invoked Collections.synchronizedMap()

or using ConcurrentHashMap

.

+4


a source







All Articles