import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set; import com.google.common.collect.Maps; public class RandomUtils {
private static Random random; //双重校验锁获取一个Random单例
public static Random getRandom() {
if(random==null){
synchronized (RandomUtils.class) {
if(random==null){
random =new Random();
}
}
} return random;
} /**
* 获得一个[0,max)之间的整数。
* @param max
* @return
*/
public static int getRandomInt(int max) {
return Math.abs(getRandom().nextInt())%max;
} /**
* 获得一个[0,max)之间的整数。
* @param max
* @return
*/
public static long getRandomLong(long max) {
return Math.abs(getRandom().nextInt())%max;
} /**
* 从list中随机取得一个元素
* @param list
* @return
*/
public static <E> E getRandomElement(List<E> list){
return list.get(getRandomInt(list.size()));
} /**
* 从set中随机取得一个元素
* @param set
* @return
*/
public static <E> E getRandomElement(Set<E> set){
int rn = getRandomInt(set.size());
int i = 0;
for (E e : set) {
if(i==rn){
return e;
}
i++;
}
return null;
} /**
* 从map中随机取得一个key
* @param map
* @return
*/
public static <K,V> K getRandomKeyFromMap(Map<K,V> map) {
int rn = getRandomInt(map.size());
int i = 0;
for (K key : map.keySet()) {
if(i==rn){
return key;
}
i++;
}
return null;
} /**
* 从map中随机取得一个value
* @param map
* @return
*/
public static <K,V> V getRandomValueFromMap(Map<K,V> map) {
int rn = getRandomInt(map.size());
int i = 0;
for (V value : map.values()) {
if(i==rn){
return value;
}
i++;
}
return null;
} public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
for (int i = 0; i < 12; i++) {
set.add(i);
} for (int i = 0; i < 10; i++) {
System.out.println(getRandomElement(set));
}
}
}

转自 https://m.2cto.com/kf/201507/412937.html

随机获取一个集合(List, Set,Map)中的元素<转>的更多相关文章

  1. 随机获取一个集合(List, Set)中的元素,随机获取一个Map中的key或value

    利用Java提供的Random类.从List或Set中随机取出一个元素,从Map中随机获取一个key或value. 因为Set没有提供get(int index)方法,仅仅能先获取一个随机数后.利用一 ...

  2. 随机获取list或set或map中的一个元素

    转自:https://m.2cto.com/kf/201507/412937.html import java.util.HashSet;import java.util.List;import ja ...

  3. scrapy实战9动态设置ip代理从数据库中随机获取一个可用的ip:

    在目录下创建tools(python package) 在tools中创建crawl_xici_ip.py文件写入代码如下: #coding=utf-8 import requests from sc ...

  4. 在ASP.NET MVC应用程序中随机获取一个字符串

    在开发ASP.NET MVC应用程序时,有可能需要一个随机字符串,作为密码或是验证码等. 如果你需要的是SQL版本,可以参考<密码需要带特殊字符(二)>http://www.cnblogs ...

  5. PHP如何随机获取一个二维数组中的一个值

    获取一个数组: $awardid_list=pdo_fetchall('select id from '.tablename($this->table_award)); 这是微擎的写法哈,意思就 ...

  6. Map四种获取key和value值的方法,以及对map中的元素排序(转)

    获取map的值主要有四种方法,这四种方法又分为两类,一类是调用map.keySet()方法来获取key和value的值,另一类则是通过map.entrySet()方法来取值,两者的区别在于,前者主要是 ...

  7. 获取一个数组里面第K大的元素

    如何在O(n)内获取一个数组比如{9, 1, 2, 8, 7, 3, 6, 4, 3, 5, 0, 9, 19, 39, 25, 34, 17, 24, 23, 34, 20}里面第K大的元素呢? 我 ...

  8. 集合练习 练习:每一个学生Student都有一个对应的归属地定义为String类型。学生属性:姓名,年龄 注意:姓名和年龄相同的视为同一个学生。保证学生的唯一性。 1、描述学生。 2、定义Map容器,将学生作为键,地址作为值存入集合中。 3、获取Map中的元素并进行排序。

    package com.rf.xs.map; public class Student implements Comparable<Student> { private String na ...

  9. 从list中取N个随机生成一个集合

    在工作中发现有很多有序算法,较少见到一些可用的无序随机算法.无序随机算法的目的是让客户感觉每次都不一样,因为一直看一样的会审美疲劳哈. 在jdk自带一种CollectionUtils.shuffle& ...

随机推荐

  1. 我们为什么要学习 Spring Boot

    现在貌似大家都知道 Spring Boot 很火了,做 Java 的不知道 Spring Boot 的都已经 Out 了,但是又有多少人是跟风学习的呢?今天我们就来说一下为什么要学习 Spring B ...

  2. vue 百度地图实现标记多个maker,并点击任意一个maker弹出对应的提示框信息, (附: 通过多个地址,标记多个marker 的 方法思路)

    通过点击不同筛选条件,筛选出不同企业所在的地点, 根据每个企业的经纬度 在地图上标记多个maker,点击任意一个maker,会弹出infoWindow 信息窗口: 说明:  因每个人写法不同.需求不同 ...

  3. Linux命令Find实例

    转自: http://www.tecmint.com/35-practical-examples-of-linux-find-command/ 35 Practical Examples of Lin ...

  4. 从安装node js到构建一个vue并启动它

    1.安装node js 下载地址:http://nodejs.cn/download/2.安装完成后运行Node.js command prompt(node -v查看安装版本) 3.安装npm(由于 ...

  5. C#中流的读写器BinaryReader、BinaryWriter,StreamReader、StreamWriter详解【转】

    https://blog.csdn.net/ymnl_gsh/article/details/80723050 C#的FileStream类提供了最原始的字节级上的文件读写功能,但我们习惯于对字符串操 ...

  6. [leetcode]Permutation Sequence @ Python

    原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...

  7. 这个Linux命令是干什么的?

    笔者遇到一个命令,觉得挺不好懂的. find . –type f –exec dd if={} of=/dev/null bs=128K status=none \; 这个命令中: {} 代表着被找到 ...

  8. Std::map too few template arguments

    在上述的代码中,红色波浪线的部分编译的时候报错: error C2976: 'std::map' : too few template arguments 换成std::map<std::str ...

  9. Visual Studio 2015 与 .NET 4.6 RTM 正式发布

    原文地址 微软终于正式发布了Visual Studio 2015产品家族的RTM版本,此次发布体现了微软在开发工具发展方向上的转变迈出了重要的一步,他们致力于提供一种支持在所有主流应用平台上进行应用开 ...

  10. SVN命令行更新代码

    命令列表 svn help查看帮助信息 Available subcommands: add auth blame (praise, annotate, ann) cat changelist (cl ...