生活随笔
收集整理的這篇文章主要介紹了
TreeMap实现权重随机数Java
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
- 項(xiàng)目開發(fā)中在很多地方需要用到權(quán)重的分配資源的功能,在做中東電商項(xiàng)目中就遇到根據(jù)語言權(quán)重來獲取系統(tǒng)中語言出現(xiàn)的權(quán)重問題,下面做一個(gè)分享自己的實(shí)現(xiàn)方式
?
- 用枚舉保存語言出現(xiàn)的權(quán)重(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 }
- 構(gòu)建一個(gè)Pair對(duì)象存儲(chǔ)權(quán)重和對(duì)應(yīng)的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實(shí)現(xiàn)存儲(chǔ)權(quán)重信息(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();
//統(tǒng)一轉(zhuǎn)為double
19 this.weightMap.put(pair.getValue().doubleValue() + lastWeight, pair.getKey());
//權(quán)重累加
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 }
- 通過權(quán)重枚舉實(shí)現(xiàn)語言權(quán)重(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 }
如果有相同的權(quán)重業(yè)務(wù)場(chǎng)景,只需要實(shí)現(xiàn)(1)中自己的權(quán)重分配枚舉,再實(shí)現(xiàn)(4)中的權(quán)重獲取方式就可以用了;這里只是做了靜態(tài)權(quán)重隨機(jī)的實(shí)現(xiàn),如果對(duì)動(dòng)態(tài)隨機(jī)感興趣的同事可以留言
轉(zhuǎn)載于:https://www.cnblogs.com/yuguojin/p/7883152.html
總結(jié)
以上是生活随笔為你收集整理的TreeMap实现权重随机数Java的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。