随机获取一个集合(List, Set,Map)中的元素<转>
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)中的元素<转>的更多相关文章
- 随机获取一个集合(List, Set)中的元素,随机获取一个Map中的key或value
利用Java提供的Random类.从List或Set中随机取出一个元素,从Map中随机获取一个key或value. 因为Set没有提供get(int index)方法,仅仅能先获取一个随机数后.利用一 ...
- 随机获取list或set或map中的一个元素
转自:https://m.2cto.com/kf/201507/412937.html import java.util.HashSet;import java.util.List;import ja ...
- scrapy实战9动态设置ip代理从数据库中随机获取一个可用的ip:
在目录下创建tools(python package) 在tools中创建crawl_xici_ip.py文件写入代码如下: #coding=utf-8 import requests from sc ...
- 在ASP.NET MVC应用程序中随机获取一个字符串
在开发ASP.NET MVC应用程序时,有可能需要一个随机字符串,作为密码或是验证码等. 如果你需要的是SQL版本,可以参考<密码需要带特殊字符(二)>http://www.cnblogs ...
- PHP如何随机获取一个二维数组中的一个值
获取一个数组: $awardid_list=pdo_fetchall('select id from '.tablename($this->table_award)); 这是微擎的写法哈,意思就 ...
- Map四种获取key和value值的方法,以及对map中的元素排序(转)
获取map的值主要有四种方法,这四种方法又分为两类,一类是调用map.keySet()方法来获取key和value的值,另一类则是通过map.entrySet()方法来取值,两者的区别在于,前者主要是 ...
- 获取一个数组里面第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大的元素呢? 我 ...
- 集合练习 练习:每一个学生Student都有一个对应的归属地定义为String类型。学生属性:姓名,年龄 注意:姓名和年龄相同的视为同一个学生。保证学生的唯一性。 1、描述学生。 2、定义Map容器,将学生作为键,地址作为值存入集合中。 3、获取Map中的元素并进行排序。
package com.rf.xs.map; public class Student implements Comparable<Student> { private String na ...
- 从list中取N个随机生成一个集合
在工作中发现有很多有序算法,较少见到一些可用的无序随机算法.无序随机算法的目的是让客户感觉每次都不一样,因为一直看一样的会审美疲劳哈. 在jdk自带一种CollectionUtils.shuffle& ...
随机推荐
- 2827: 千山鸟飞绝 非旋treap
国际惯例的题面:看起来很不可做的样子,我们先来整理一下题意吧.就是,维护每个点曾经拥有过的最大的两个属性值,支持把点的位置移动.我们用map对每个位置进行离散化,对每个位置建立一个平衡树.为了方便分离 ...
- HDU3915 Game 高斯消元
题目链接 HDU3915 Game 高斯消元 题解 求异或方程组自由元的子集个数 高斯消元求秩,内存溢出好神 代码 #include<bitset> #include<cstdio& ...
- 洛谷.2234.[HNOI2002]营业额统计(Splay)
题目链接 //模板吧 #include<cstdio> #include<cctype> #include<algorithm> using namespace s ...
- 探究functools模块wraps装饰器的用途
<A Byte of Python>17.8节讲decorator的时候,用到了functools模块中的一个装饰器:wraps.因为之前没有接触过这个装饰器,所以特地研究了一下. 何谓“ ...
- Python3练习题系列(04)
题目: 制作一个游戏 知识点: 函数.if_elif_else, while, exit 游戏图谱: 游戏代码: from sys import exit def gold_room(): print ...
- Java开发环境安装过程
IntelliJ IDEA 安装 下载 配置代理信息 JDK 安装 安装JDK,cmd -> java -version 查看是否有java版本信息安装成功会显示版本信息 配置环境变量Path, ...
- yum 安装 jenkins
环境:已安装 tomcat 安装(如果yum下载速度比较忙可以下载下来再安装) yum -y install https://pkg.jenkins.io/redhat/jenkins-2.77-1. ...
- oracle 列相减——(Oracle分析函数Lead(),Lag())
lag和lead函数,用于取出数据的前n行的数据和后n行的数据,当然要和over(order by)一起组合 其实这2个函数的作用非常好理解,Lead()就是取当前顺序的下一条记录,相对Lag()就是 ...
- db2执行计划具体操作
explain 1.如果第一次执行,请先(在dbinst用户下) connect to dbname,执行db2 -tvf $HOME/sqllib/misc/EXPLAIN.DDL建立执行计划表 2 ...
- Knockout.Js官网学习(checked 绑定)
前言 checked绑定是关联到checkable的form表单控件到view model上 - 例如checkbox(<input type='checkbox'>)或者radio bu ...