1. 程式人生 > >【Java】隨機n個list的值

【Java】隨機n個list的值

Map map = new HashMap();
List listNew = new ArrayList();
if (list.size() <= n) {
   return gson.toJson(list);
} else {
   while (map.size() < n) {
       int random = (int) (Math.random() * list.size());
       if (!map.containsKey(random)) {
          map.put(random, "");
          System.out.println(random + "===========" + list.get(random));
          listNew.add(list.get(random));
        }
   }
}