【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));
        }
   }
}

猜你喜欢

转载自blog.csdn.net/wjx_jasin/article/details/84952564