###### JAVA权重抽取

1.

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.wiimedia.mryl.userportrait.bean.CalcWeightBean;

public class CalcWeightUtil {
public  Map<String, Integer> getCalc(List<CalcWeightBean> beans){
Map<String, Integer> showMap = null;
Double sum = getSum(beans);
Double random = 0.0;
CalcWeightBean kw = null;
for(int i = 0; i < 7; i++) {
random = getRandom(sum);
CalcWeightBean cw = getKW(beans, random);
if(showMap.containsKey(cw.getTag())) {
showMap.put(cw.getTag(), showMap.get(cw.getTag()) + 1);
} else {
showMap.put(cw.getTag(),1);
}
}
return showMap;
}

public  CalcWeightBean getKW(List<CalcWeightBean> nodes, Double rd) {
CalcWeightBean ret = null;
int curWt = 0;
for(CalcWeightBean n : nodes){
curWt += n.getWeight();
if(curWt >= rd) {
ret = n;
break;
}
}
return ret;
}
public static Double getSum(List<CalcWeightBean> nodes) {
Double sum = 0.0;
for(CalcWeightBean n : nodes)
sum += n.getWeight();
return sum;
}
public static Double getRandom(Double seed) {
return (double)Math.round(Math.random() * seed);
}

}

2.

import java.util.Comparator;

/**
*
* @author Songjia
*
* @作用： 进行权重排序的Javabean
*
* @时间：2016-05-06 19:55
*
*/
public class CalcWeightBean implements Comparator{
private double weight;
private String tag;
public CalcWeightBean() {
super();
}
public CalcWeightBean(double weight, String tag) {
super();
this.weight = weight;
this.tag = tag;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public int compare(Object o1, Object o2) {
CalcWeightBean n1 = (CalcWeightBean)o1;
CalcWeightBean n2 = (CalcWeightBean)o2;
if(n1.weight > n2.weight)
return 1;
else
return 0;

}

}

3.测试类

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.wiimedia.mryl.userportrait.bean.CalcWeightBean;
import com.wiimedia.mryl.utils.CalcWeightUtil;

public class Weight {
public static void main(String[] args) {
CalcWeightUtil calcWeightUtil = new CalcWeightUtil();
CalcWeightBean bean1= new CalcWeightBean(31.25,"鼻炎");
CalcWeightBean bean2= new CalcWeightBean(18.75,"高血压");
CalcWeightBean bean3= new CalcWeightBean(18.75,"癌症");
CalcWeightBean bean4= new CalcWeightBean(31.25,"糖尿病");
CalcWeightBean bean5= new CalcWeightBean(31.25,"关节炎");
List<CalcWeightBean> beans = new ArrayList<CalcWeightBean>();

Map<String, Integer> calc = calcWeightUtil.getCalc(beans);
System.out.println(calc);
for (Map.Entry<String, Integer> entry : calc.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
}
}

#### 计算文章中每个词的权重值-信息熵及代码实现

2016-06-29 16:15:32

#### Java实现根据权重优先返回（速度较快）

2016-08-12 10:58:27

#### 随机选取算法 (有权重的记录中选取)

2015-05-20 01:23:18

#### 根据权重随机读取数据

2016-02-22 13:09:55

#### 按权重随机获取相应的数据

2016-02-02 19:22:34

#### java版根据权重抽奖算法

2015-08-04 15:19:56

#### \t\tC# 权重控制随机抽取率

2013-06-09 15:31:50

#### 按概率（权重）抽取（显示）

2018-03-05 10:26:57

#### 带权重的随机算法

2016-04-03 19:42:41

#### java实现权重随机算法

2016-10-11 09:54:53

JAVA权重抽取