/** * 从list中随机抽取元素 * * @param list * @param n * @return void * @throws * @Title: createRandomList * @Description: TODO */ private static List createRandomList(List list, int n) { // TODO Auto-generated method stub Map map = new HashMap(); List listNew = new ArrayList(); if (list.size() <= n) { return 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)); } } return listNew; } }12345678910111213141516171819202122232425262728
扩展:
截取list
list.subList(0, 2);