Hacker News new | ask | show | jobs
by billnapier 5835 days ago
HashMap (unlike treeset) itself doesn't provide an order guarantee. So can't you just grab the first element from the entrySet and use that?

But I think the bigger problem is trying to use a Map in the first place when maybe a different data structure will solve the problem better.

2 comments

I of course didn't talk about why I'm using a map, but as an example: consider redis. It's a giant hash table, and it allows you to pick a random key.
Just because it doesn't make any guarantees about order, doesn't mean you'll get a randomly-selected element if you pick the first one.