Java實現(xiàn)權(quán)重隨機獲取值或?qū)ο蟮姆椒?/h1>
更新時間:2023年11月08日 09:45:35 作者:你就像甜甜的益達
這篇文章主要介紹了Java實現(xiàn)權(quán)重隨機獲取值或?qū)ο蟮姆椒?treeMap是一種基于紅黑樹實現(xiàn)的有序映射表,提供了一系列的方法來操作映射表中的元素,其中tailMap方法是用于返回映射表中大于或等于給定鍵的部分視圖,需要的朋友可以參考下
場景
按照權(quán)重2,8給用戶分組為A,B,
TreeMap.tailMap方法
treeMap是一種基于紅黑樹實現(xiàn)的有序映射表,提供了一系列的方法來操作映射表中的元素。其中tailMap方法是用于返回映射表中大于或等于給定鍵的部分視圖。
tailMap方法的定義如下:
public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
return new AscendingSubMap<>(this,
false, fromKey, inclusive,
true, null, true);
}
其中,fromKey表示起始鍵,返回一個從fromKey開始到映射表末尾的部分視圖。inclusive是表示是否包含傳入的fronKey.這個部分視圖是SortedMap類型的,可以進行排序操作。
使用tailMap方法需要注意以下幾點:
- 如果fromKey不存在于映射表中,則返回的部分視圖將包含大于fromKey的所有鍵值對。
- 返回的部分視圖是映射表的一個視圖,對這個視圖所做的修改會影響到原映射表。
- 返回的部分視圖是有序的,可以進行排序操作。
下面是一個示例代碼:
import java.util.TreeMap;
import java.util.SortedMap;
public class TreeMapExample {
public static void main(String[] args) {
TreeMap<Integer, String> treeMap = new TreeMap<>();
treeMap.put(1, "one");
treeMap.put(2, "two");
treeMap.put(3, "three");
treeMap.put(4, "four");
treeMap.put(5, "five");
// 返回大于等于3的部分視圖
SortedMap<Integer, String> tailMap = treeMap.tailMap(3);
System.out.println(tailMap); // 輸出 {3=three, 4=four, 5=five}
// 修改部分視圖
tailMap.put(6, "six");
System.out.println(treeMap); // 輸出 {1=one, 2=two, 3=three, 4=four, 5=five, 6=six}
// 對部分視圖進行排序
SortedMap<Integer, String> sortedTailMap = tailMap.descendingMap();
System.out.println(sortedTailMap); // 輸出 {6=six, 5=five, 4=four, 3=three}
}
}
在上面的示例代碼中,首先創(chuàng)建了一個treeMap對象,并向其中添加了5個鍵值對。然后使用tailMap方法返回了大于等于3的部分視圖,并對這個部分視圖進行了修改和排序操作。
tailMap方法是Java中treeMap類提供的一個非常有用的方法,可以方便地獲取映射表中大于等于指定鍵的部分視圖,并進行排序和修改操作。 針對這個特性可以用來獲取權(quán)重值
簡單分析
好比A:B的權(quán)重為2:8,那么相當(dāng)于A的權(quán)重為0->2,B的權(quán)重為2->10(2+8)都是包左不包右的; 那我們就可以隨機個0-10的值,如果在0->2那么返回A,如果2->10那就返回B
使用隨機值
public static String test1() {
Random random = new Random();
int i = random.nextInt(10);
if (i < 2) {
return "A";
} else if (i >= 2 && i < 10) {
return "B";
} else {
return "C";
}
}
使用treemap實現(xiàn)權(quán)重取值
public static String test2() {
TreeMap<Integer, String> treeMap = new TreeMap<>();
int total = 2 + 8;
treeMap.put(2, "A");
treeMap.put(total, "B");
Random random = new Random();
return treeMap.tailMap(random.nextInt(total), false).firstEntry().getValue();
}
將Int改為Double稍微準確一點,因為double隨機的值更加多
public static String test3() {
TreeMap<Double, String> treeMap = new TreeMap<>();
int total = 2 + 8;
treeMap.put((double) 2, "A");
treeMap.put((double) total, "B");
Random random = new Random();
return treeMap.tailMap(total * random.nextDouble(), false).firstEntry().getValue();
}
測試main方法
package com.study.springbootplus.test;
import cn.hutool.core.lang.WeightRandom;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.TreeMap;
/**
* @ClassName RandomTest
* @Author yida
* @Date 2023-09-14 18:26
* @Description RandomTest
*/
public class RandomTest {
public static void main(String[] args) {
int num_a = 0, num_b = 0, num_c = 0;
int testCount = 1000;
for (int i = 0; i < testCount; i++) {
switch (test3()) {
case "A":
num_a = num_a + 1;
break;
case "B":
num_b = num_b + 1;
break;
case "C":
num_c = num_c + 1;
break;
}
}
System.out.println("A-" + num_a + "-------" + NumberUtil.div(num_a, testCount, 2) * 100 + "%");
System.out.println("B-" + num_b + "-------" + NumberUtil.div(num_b, testCount, 2) * 100 + "%");
System.out.println("C-" + num_c + "-------" + NumberUtil.div(num_c, testCount, 2) * 100 + "%");
}
public static String test1() {
Random random = new Random();
int i = random.nextInt(10);
if (i < 2) {
return "A";
} else if (i >= 2 && i < 10) {
return "B";
} else {
return "C";
}
}
public static String test2() {
TreeMap<Integer, String> treeMap = new TreeMap<>();
int total = 2 + 8;
treeMap.put(2, "A");
treeMap.put(total, "B");
Random random = new Random();
return treeMap.tailMap(random.nextInt(total), false).firstEntry().getValue();
}
public static String test3() {
TreeMap<Double, String> treeMap = new TreeMap<>();
int total = 2 + 8;
treeMap.put((double) 2, "A");
treeMap.put((double) total, "B");
Random random = new Random();
return treeMap.tailMap(total * random.nextDouble(), false).firstEntry().getValue();
}
public static void test() {
List<WeightRandom.WeightObj<String>> weightList = new ArrayList<>();
weightList.add(new WeightRandom.WeightObj<>("A", 20));
weightList.add(new WeightRandom.WeightObj<>("B", 30));
weightList.add(new WeightRandom.WeightObj<>("C", 40));
weightList.add(new WeightRandom.WeightObj<>("D", 10));
WeightRandom<String> wr = RandomUtil.weightRandom(weightList);
String str = "";
int num_a = 0, num_b = 0, num_c = 0, num_d = 0;
int testCount = 10000;
for (int i = 0; i < testCount; i++) {
str = wr.next();
switch (str) {
case "A":
num_a = num_a + 1;
break;
case "B":
num_b = num_b + 1;
break;
case "C":
num_c = num_c + 1;
break;
case "D":
num_d = num_d + 1;
break;
}
}
System.out.println("A-" + num_a + "-------" + NumberUtil.div(num_a, testCount, 2) * 100 + "%");
System.out.println("B-" + num_b + "-------" + NumberUtil.div(num_b, testCount, 2) * 100 + "%");
System.out.println("C-" + num_c + "-------" + NumberUtil.div(num_c, testCount, 2) * 100 + "%");
System.out.println("D-" + num_d + "-------" + NumberUtil.div(num_d, testCount, 2) * 100 + "%");
}
}
測試結(jié)果:
A-195-------20.0%
B-805-------81.0%
C-0-------0.0%
當(dāng)權(quán)重的參數(shù)比較多,那么建議使用hutool封裝的
到此這篇關(guān)于Java實現(xiàn)權(quán)重隨機獲取值或?qū)ο蟮姆椒ǖ奈恼戮徒榻B到這了,更多相關(guān)Java權(quán)重隨機獲取值或?qū)ο髢?nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
-
SpringMVC前端和后端數(shù)據(jù)交互總結(jié)
本篇文章主要介紹了SpringMVC前端和后端數(shù)據(jù)交互總結(jié),具有一定的參考價值,感興趣的小伙伴們可以參考一下。
2017-03-03
-
SpringBoot Admin2.0 集成Arthas的實現(xiàn)步驟
這篇文章主要介紹了SpringBoot Admin2.0 集成Arthas的實現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下 2021-04-04
最新評論
場景
按照權(quán)重2,8給用戶分組為A,B,
TreeMap.tailMap方法
treeMap是一種基于紅黑樹實現(xiàn)的有序映射表,提供了一系列的方法來操作映射表中的元素。其中tailMap方法是用于返回映射表中大于或等于給定鍵的部分視圖。
tailMap方法的定義如下:
public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) { return new AscendingSubMap<>(this, false, fromKey, inclusive, true, null, true); }
其中,fromKey表示起始鍵,返回一個從fromKey開始到映射表末尾的部分視圖。inclusive是表示是否包含傳入的fronKey.這個部分視圖是SortedMap類型的,可以進行排序操作。
使用tailMap方法需要注意以下幾點:
- 如果fromKey不存在于映射表中,則返回的部分視圖將包含大于fromKey的所有鍵值對。
- 返回的部分視圖是映射表的一個視圖,對這個視圖所做的修改會影響到原映射表。
- 返回的部分視圖是有序的,可以進行排序操作。
下面是一個示例代碼:
import java.util.TreeMap; import java.util.SortedMap; public class TreeMapExample { public static void main(String[] args) { TreeMap<Integer, String> treeMap = new TreeMap<>(); treeMap.put(1, "one"); treeMap.put(2, "two"); treeMap.put(3, "three"); treeMap.put(4, "four"); treeMap.put(5, "five"); // 返回大于等于3的部分視圖 SortedMap<Integer, String> tailMap = treeMap.tailMap(3); System.out.println(tailMap); // 輸出 {3=three, 4=four, 5=five} // 修改部分視圖 tailMap.put(6, "six"); System.out.println(treeMap); // 輸出 {1=one, 2=two, 3=three, 4=four, 5=five, 6=six} // 對部分視圖進行排序 SortedMap<Integer, String> sortedTailMap = tailMap.descendingMap(); System.out.println(sortedTailMap); // 輸出 {6=six, 5=five, 4=four, 3=three} } }
在上面的示例代碼中,首先創(chuàng)建了一個treeMap對象,并向其中添加了5個鍵值對。然后使用tailMap方法返回了大于等于3的部分視圖,并對這個部分視圖進行了修改和排序操作。
tailMap方法是Java中treeMap類提供的一個非常有用的方法,可以方便地獲取映射表中大于等于指定鍵的部分視圖,并進行排序和修改操作。 針對這個特性可以用來獲取權(quán)重值
簡單分析
好比A:B的權(quán)重為2:8,那么相當(dāng)于A的權(quán)重為0->2,B的權(quán)重為2->10(2+8)都是包左不包右的; 那我們就可以隨機個0-10的值,如果在0->2那么返回A,如果2->10那就返回B
使用隨機值
public static String test1() { Random random = new Random(); int i = random.nextInt(10); if (i < 2) { return "A"; } else if (i >= 2 && i < 10) { return "B"; } else { return "C"; } }
使用treemap實現(xiàn)權(quán)重取值
public static String test2() { TreeMap<Integer, String> treeMap = new TreeMap<>(); int total = 2 + 8; treeMap.put(2, "A"); treeMap.put(total, "B"); Random random = new Random(); return treeMap.tailMap(random.nextInt(total), false).firstEntry().getValue(); }
將Int改為Double稍微準確一點,因為double隨機的值更加多
public static String test3() { TreeMap<Double, String> treeMap = new TreeMap<>(); int total = 2 + 8; treeMap.put((double) 2, "A"); treeMap.put((double) total, "B"); Random random = new Random(); return treeMap.tailMap(total * random.nextDouble(), false).firstEntry().getValue(); }
測試main方法
package com.study.springbootplus.test; import cn.hutool.core.lang.WeightRandom; import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.RandomUtil; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.TreeMap; /** * @ClassName RandomTest * @Author yida * @Date 2023-09-14 18:26 * @Description RandomTest */ public class RandomTest { public static void main(String[] args) { int num_a = 0, num_b = 0, num_c = 0; int testCount = 1000; for (int i = 0; i < testCount; i++) { switch (test3()) { case "A": num_a = num_a + 1; break; case "B": num_b = num_b + 1; break; case "C": num_c = num_c + 1; break; } } System.out.println("A-" + num_a + "-------" + NumberUtil.div(num_a, testCount, 2) * 100 + "%"); System.out.println("B-" + num_b + "-------" + NumberUtil.div(num_b, testCount, 2) * 100 + "%"); System.out.println("C-" + num_c + "-------" + NumberUtil.div(num_c, testCount, 2) * 100 + "%"); } public static String test1() { Random random = new Random(); int i = random.nextInt(10); if (i < 2) { return "A"; } else if (i >= 2 && i < 10) { return "B"; } else { return "C"; } } public static String test2() { TreeMap<Integer, String> treeMap = new TreeMap<>(); int total = 2 + 8; treeMap.put(2, "A"); treeMap.put(total, "B"); Random random = new Random(); return treeMap.tailMap(random.nextInt(total), false).firstEntry().getValue(); } public static String test3() { TreeMap<Double, String> treeMap = new TreeMap<>(); int total = 2 + 8; treeMap.put((double) 2, "A"); treeMap.put((double) total, "B"); Random random = new Random(); return treeMap.tailMap(total * random.nextDouble(), false).firstEntry().getValue(); } public static void test() { List<WeightRandom.WeightObj<String>> weightList = new ArrayList<>(); weightList.add(new WeightRandom.WeightObj<>("A", 20)); weightList.add(new WeightRandom.WeightObj<>("B", 30)); weightList.add(new WeightRandom.WeightObj<>("C", 40)); weightList.add(new WeightRandom.WeightObj<>("D", 10)); WeightRandom<String> wr = RandomUtil.weightRandom(weightList); String str = ""; int num_a = 0, num_b = 0, num_c = 0, num_d = 0; int testCount = 10000; for (int i = 0; i < testCount; i++) { str = wr.next(); switch (str) { case "A": num_a = num_a + 1; break; case "B": num_b = num_b + 1; break; case "C": num_c = num_c + 1; break; case "D": num_d = num_d + 1; break; } } System.out.println("A-" + num_a + "-------" + NumberUtil.div(num_a, testCount, 2) * 100 + "%"); System.out.println("B-" + num_b + "-------" + NumberUtil.div(num_b, testCount, 2) * 100 + "%"); System.out.println("C-" + num_c + "-------" + NumberUtil.div(num_c, testCount, 2) * 100 + "%"); System.out.println("D-" + num_d + "-------" + NumberUtil.div(num_d, testCount, 2) * 100 + "%"); } }
測試結(jié)果:
A-195-------20.0%
B-805-------81.0%
C-0-------0.0%
當(dāng)權(quán)重的參數(shù)比較多,那么建議使用hutool封裝的
到此這篇關(guān)于Java實現(xiàn)權(quán)重隨機獲取值或?qū)ο蟮姆椒ǖ奈恼戮徒榻B到這了,更多相關(guān)Java權(quán)重隨機獲取值或?qū)ο髢?nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC前端和后端數(shù)據(jù)交互總結(jié)
本篇文章主要介紹了SpringMVC前端和后端數(shù)據(jù)交互總結(jié),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03SpringBoot Admin2.0 集成Arthas的實現(xiàn)步驟
這篇文章主要介紹了SpringBoot Admin2.0 集成Arthas的實現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下2021-04-04