Is there a way to get the value of a HashMap randomly in Java?
This works: Random generator = new Random(); Object[] values = myHashMap.values().toArray(); Object randomValue = values[generator.nextInt(values.length)]; If you want the random value to be a type other than an Object simply add a cast to the last line. So if myHashMap was declared as: Map<Integer,String> myHashMap = new HashMap<Integer,String>(); The last line can be: String randomValue … Read more