4 ms·
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 b
by billnapier 16y 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.
- apgwoz 16y agoI 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.
- nervechannel 16y agoJust 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.