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

前端 未结 13 2121
心在旅途
心在旅途 2020-11-30 03:45

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

13条回答
  •  萌比男神i
    2020-11-30 04:29

    Usually you do not really want a random value but rather just any value, and then it's nice doing this:

    Object selectedObj = null;
    for (Object obj : map.values()) {
        selectedObj = obj;
        break;
    }
    

提交回复
热议问题