0

Possible Duplicate:
Is there a way to get the value of a HashMap randomly in Java?

I want to generate random keys and show their associated values in a Hashmap but not all the keys. For example if there are 40 key value pairs I just want 10 to be randomly displayed.

Community
  • 1
  • 1

1 Answers1

2
List keys = new ArrayList(theHashMap.keySet());
Collections.shuffle(keys);
for (int i = 0; i < Math.min(keys.size(), 10); ++i) {
  map.get(keys[i]);
}
jspcal
  • 50,847
  • 7
  • 72
  • 76