生活随笔
收集整理的这篇文章主要介绍了
TreeMap实现权重随机数Java
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
- 项目开发中在很多地方需要用到权重的分配资源的功能,在做中东电商项目中就遇到根据语言权重来获取系统中语言出现的权重问题,下面做一个分享自己的实现方式
1 /**
2 * @author yuguojin
3 */
4 public enum CommentLangIdEnum {
5
6 ENGLISH(1, "英语", 15
),
7
8 ARABIC(2, "阿拉伯语", 85
);
9
10 private int value;
11
12 private String code;
13
14 private int weight;
15
16 CommentLangIdEnum(
int value, String code,
int weight) {
17 this.code =
code;
18 this.value =
value;
19 this.weight =
weight;
20 }
21
22 public int getValue() {
23 return value;
24 }
25
26 public String getCode() {
27 return code;
28 }
29
30 public int getWeight() {
31 return weight;
32 }
33
34 }
1 /**
2 * @author yuguojin
3 * @param <K>
4 * @param <V>
5 */
6 public class Pair<K, V>
{
7
8 private K key;
9
10 private V value;
11
12 public Pair(K key, V value) {
13 this.key =
key;
14 this.value =
value;
15 }
16
17 public K getKey() {
18 return key;
19 }
20
21 public V getValue() {
22 return value;
23 }
24 }
1 import java.util.List;
2 import java.util.SortedMap;
3 import java.util.TreeMap;
4
5 import com.google.common.base.Preconditions;
6
7 /**
8 * @author yuguojin
9 * @param <K>
10 * @param <V>
11 */
12 public class WeightRandom<K, V
extends Number>
{
13 private TreeMap<Double, K> weightMap =
new TreeMap<Double, K>
();
14
15 public WeightRandom(List<Pair<K, V>>
list) {
16 Preconditions.checkNotNull(list, "list can NOT be null!"
);
17 for (Pair<K, V>
pair : list) {
18 double lastWeight =
this.weightMap.size() == 0 ? 0 :
this.weightMap.lastKey().doubleValue();
//统一转为double
19 this.weightMap.put(pair.getValue().doubleValue() + lastWeight, pair.getKey());
//权重累加
20 }
21 }
22
23 public K random() {
24 double randomWeight =
this.weightMap.lastKey() *
Math.random();
25 SortedMap<Double, K> tailMap =
this.weightMap.tailMap(randomWeight,
false);
26 return this.weightMap.get(tailMap.firstKey());
27 }
28
29 }
1 import java.util.ArrayList;
2 import java.util.List;
3
4 import org.springframework.stereotype.Component;
5
6 import com.appollo.product.common.enums.CommentLangIdEnum;
7
8 /**
9 *
10 * @author yuguojin
11 *
12 */
13 @Component
14 public class CommentLangRandom {
15
16 private static volatile WeightRandom<Integer, Integer>
randomLanguageId;
17
18 public int getLanguageId() {
19 initWeightRandom();
20 return randomLanguageId.random();
21 }
22
23 private void initWeightRandom() {
24 if (
null ==
randomLanguageId) {
25 synchronized (
this) {
26 if (
null ==
randomLanguageId) {
27 randomLanguageId =
new WeightRandom<>
(initStarsPair());
28 }
29 }
30 }
31 }
32
33 private List<Pair<Integer, Integer>>
initStarsPair() {
34 List<Pair<Integer, Integer>> starsPair =
new ArrayList<Pair<Integer, Integer>>
();
35 for (CommentLangIdEnum starEnum : CommentLangIdEnum.values()) {
36 Pair<Integer, Integer> starPair =
new Pair<Integer, Integer>
(starEnum.getValue(), starEnum.getWeight());
37 starsPair.add(starPair);
38 }
39
40 return starsPair;
41 }
42 }
如果有相同的权重业务场景,只需要实现(1)中自己的权重分配枚举,再实现(4)中的权重获取方式就可以用了;这里只是做了静态权重随机的实现,如果对动态随机感兴趣的同事可以留言
转载于:https://www.cnblogs.com/yuguojin/p/7883152.html
总结
以上是生活随笔为你收集整理的TreeMap实现权重随机数Java的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。