ConcurrentSkipListMap实现随机数命中权重占比
public String getCoverBottom(FlowAdditions additions) { if(null==additions){ return "默认"; } List<FlowCoverBottom> li = this.isMatchBottom(additions); if(null == li || li.isEmpty()){ return "默认"; } //根据权重输出兜底方。 int sum = li.stream().mapToInt(FlowCoverBottom::getIntRatio).sum(); int random = RandomUtils.nextInt(0, sum); ConcurrentSkipListMap<Integer,String> skipListMap = new ConcurrentSkipListMap<>(); int ceiling = 0; for(int i=0;i<li.size();i++){ FlowCoverBottom bottom = li.get(i); ceiling = ceiling+bottom.getIntRatio(); skipListMap.put(ceiling,bottom.getCode()); } return skipListMap.higherEntry(random).getValue(); }