java list随机抽取元素

2018-03-12 23:18:12
/**
     * 从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);


  • 2021-06-08 11:20:55

    安装iohook出错

    no such file or directory, open 'C:\Users\me\AppData\Local\Temp\prebuild.tar.gz'

  • 2021-08-13 23:10:31

    ios framework中的图片如何访问

    特别简单,在主工程设置中,只要resource parse加入那个图片文件夹或者图片目录就可以正常访问了

  • 2021-08-13 23:15:04

    最新iOS APP打包上传到AppStore教程

    作为一名iOS开发者,把辛辛苦苦开发出来的App上传到App Store是件必要的事。但是很多人还是不知道该怎么上传到App Store上,下面就来详细讲解一下具体流程步骤。