Knowing the size of the map, you could pick a random entry number, then iterate over the contents until you reach that entry. Example:
final Set<String> keys = allowedInput.keySet();
final int keyNumber = (int)(Math.random() * keys.size());
final Iterator<String> keyIterator = keys.iterator();
String randomKey = null;
for (int i = 0; i < keyNumber && keyIterator.hasNext(); i++) {
randomKey = keyIterator.next();
}
if (randomKey == null) {
// This should not happen unless the map was empty, or it was modified
// externally. Handle the potential error case accordingly.
}
final HashSet<Integer> value = allowedInput.get(randomKey);
// `value` now contains a random element from the `allowedInput` map.
If you want to retrieve a random Integer
element from the resulting HashSet<Integer>
, then you can adapt the same technique: simply pick a random element number based on the size of the set, and iterate over the contents until you find it.