日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/9 编程问答 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java map 随机取值_随机获取一个集合(List, Set)中的元素,随机获取一个Map中的key或value... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

利用Java提供的Random類。從List或Set中隨機取出一個元素,從Map中隨機獲取一個key或value。

因為Set沒有提供get(int index)方法,僅僅能先獲取一個隨機數后。利用一個計數器,對Set進行循環,當計數器等于隨機數時返回當前元素,對于Map的處理也類似。

不知有沒有更好的方法……

package com.xjj.util;

import java.util.List;

import java.util.Map;

import java.util.Set;

import java.util.concurrent.ThreadLocalRandom;

/**

* 隨機數工具,單例模式

* @author XuJijun

*

*/

public class RandomUtils {

//private static Random random;

//雙重校驗鎖獲取一個Random單例

public static ThreadLocalRandom getRandom() {

return ThreadLocalRandom.current();

/*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 getRandom().nextInt(max);

}

/**

* 獲得一個[min, max]之間的隨機整數

* @param min

* @param max

* @return

*/

public static int getRandomInt(int min, int max) {

return getRandom().nextInt(max-min+1) + min;

}

/**

* 獲得一個[0,max)之間的長整數。

* @param max

* @return

*/

public static long getRandomLong(long max) {

return getRandom().nextLong(max);

}

/**

* 從數組中隨機獲取一個元素

* @param array

* @return

*/

public static E getRandomElement(E[] array){

return array[getRandomInt(array.length)];

}

/**

* 從list中隨機取得一個元素

* @param list

* @return

*/

public static E getRandomElement(List list){

return list.get(getRandomInt(list.size()));

}

/**

* 從set中隨機取得一個元素

* @param set

* @return

*/

public static E getRandomElement(Set 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 getRandomKeyFromMap(Map 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 V getRandomValueFromMap(Map map) {

int rn = getRandomInt(map.size());

int i = 0;

for (V value : map.values()) {

if(i==rn){

return value;

}

i++;

}

return null;

}

/**

* 生成一個n位的隨機數,用于驗證碼等

* @param n

* @return

*/

public static String getRandNumber(int n) {

String rn = "";

if (n > 0 && n < 10) {

//Random r = new Random();

StringBuffer str = new StringBuffer();

for (int i = 0; i < n; i++) {

str.append('9');

}

int num = Integer.parseInt(str.toString());

while (rn.length() < n) {

rn = String.valueOf(ThreadLocalRandom.current().nextInt(num));

}

} else {

rn = "0";

}

return rn;

}

public static void main(String[] args) {

/*Set set = new HashSet<>();

for (int i = 0; i < 12; i++) {

set.add("I am: " + i);

}

for (int i = 0; i < 10; i++) {

System.out.println(getRandomElement(set));

}*/

System.out.println(getRandom().nextInt(-100, 10));

}

}

總結

以上是生活随笔為你收集整理的java map 随机取值_随机获取一个集合(List, Set)中的元素,随机获取一个Map中的key或value...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。