I'm implementing a custom hash function, If I get a number of collisions into a HashMap bucket, how can I know how many elements are stored in the bucket?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
There's no builtin way to determine if a collision occurred. You will have to investigate how the collection (HashMap) distributes hashCode values to buckets and mirror the process yourself, monitoring your inserts to keep track of collisions.
you could write some reflective code to gain access to the internal buckets of the HashMap and inspect them yourself.
There is no direct support for this in the API. The member variable
table
, used for storing the buckets, is not even public, so extending the class won't get you far.Assuming you're evaluating hash functions and not doing this in production code, you can get passed these constraints using reflection.
I managed to print the content of the buckets. To analyze the distribution metrics shouldn't be hard from this point. Here's the code:
Test driver:
SubHashMap:
Output: