Is there a way to get the value of a HashMap randomly in Java?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This works:
If you want the random value to be a type other than an
Objectsimply add a cast to the last line. So ifmyHashMapwas declared as:The last line can be:
The below doesn’t work,
Set.toArray()always returns an array ofObjects, which can’t be coerced into an array ofMap.Entry.