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


  • 2019-11-04 02:12:42

    genymotion免费版

    genymotion是一套完整的android虚拟环境工具,相对官方android sdk,它的安装和使用简洁方便不臃肿,但是现在genymotion官方网站不再提供个人的下载,这篇博客记录了如何下载和使用genymotion个人版.