TreeMap实现权重随机数Java

  • 项目开发中在很多地方需要用到权重的分配资源的功能,在做中东电商项目中就遇到根据语言权重来获取系统中语言出现的权重问题,下面做一个分享自己的实现方式

 

  • 用枚举保存语言出现的权重(1)
 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 }
  • 构建一个Pair对象存储权重和对应的Key(2)
 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 }
  • 利用TreeMap实现存储权重信息(3)
 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 }
  • 通过权重枚举实现语言权重(4)
 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值