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

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

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

13条回答
  •  眼角桃花
    2020-11-30 04:17

    Should you need to draw futher values from the map without repeating any elements you can put the map into a List and then shuffle it.

    List valuesList = new ArrayList(map.values());
    Collections.shuffle( valuesList );
    
    for ( Object obj : valuesList ) {
        System.out.println( obj );
    }
    
        

    提交回复
    热议问题