java list中随机取出几条数据,list中随机取出1条数据,list中随机取出一条数据,list中随机取出3条数据,list中随机取出三条数据

作者:施自扬
微信号:sszzyy123aabbcc

Java:list中随机取出几条数据

1.公用方法:

参数:list 集合,count 要取的条数
在这里插入图片描述

2.请求的main方法:

在这里插入图片描述

3.代码复制:

public static List GetRandomThreeInfoList(List list, int count) { List olist = new ArrayList<>(); if (list.size() <= count) { return list; } else { Random random = new Random(); for (int i = 0 ;i<count;i++){ int intRandom = random.nextInt(list.size() - 1); olist.add(list.get(intRandom)); list.remove(list.get(intRandom)); } return olist; } }