Skip to content

Conversation

@stevenschlansker
Copy link

  • Relies on implementation of all Map objects to be HashMap
  • Relies on implementation details of HashMap which changed for Java 8, so the code no longer works
  • Very poor randomness - because it selects a bucket first, colliding hashcodes will cause elements to be selected with dramatically reduced frequency

Biggest downside is selecting a random element is now O(N). Most unfortunate, but a slower working solution is better than a broken fast one...

* Relies on implementation of all Map objects to be HashMap
* Relies on implementation details of HashMap which changed for Java 8, so the code no longer works
* Very poor randomness - because it selects a bucket first, colliding hashcodes will cause elements to be selected with dramatically reduced frequency
@spullara
Copy link
Owner

Will check it out. Original was copied from Redis as one thing that it documents is that the algorithm is O(1).

http://redis.io/commands/randomkey
https://github.com/antirez/redis/blob/4d2e8fa189665e8e76040da9642ca89ef55fba10/src/dict.c#L652

@stevenschlansker
Copy link
Author

@spullara any chance this could get looked at sometime soon? It means the library is broken on JDK8 or later.

@spullara
Copy link
Owner

I'm pretty sure I need to stick with the way Redis works. It is unacceptable to replace an O(1) algorithm with an O(n) algorithm... Not sure what to do yet for Java 8, probably have to find another HashMap implementation.

@stevenschlansker
Copy link
Author

It's somewhat unfortunate that the way it achieves O(1) is by making the algorithm incorrect... (as the distribution is not actually random in the presence of hash collisions)

@spullara
Copy link
Owner

Oh I agree that is totally not random and unfortunate. My guess is that since people often have millions of keys in Redis that the alternative was untenable.

@benhardy
Copy link

Given that as of this month Java 7 will no longer be getting updates, this is becoming increasingly problematic. Having such useful code be unavailable to users of current JDKs is less than ideal for many and downright awful for some.

How about let's figure out how we can fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants