创建maven项目
依赖
cn.hutool hutool-all 5.7.16 com.google.code.google-collections google-collect snapshot-20080530
实体类
package com.swy.math;
import lombok.Data;
import java.io.Serializable
@Data
public class SwyShareBO implements Serializable {
private static final long serialVersionUID = 1L;
private Integer key;
private Integer count;
}
BigDecimal比较工具
package com.swy.math;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.math.BigDecimal;
public class BigDecimalCompareUtils {
@Getter
@AllArgsConstructor
public enum CompareResultEnum {
// -1, 0, or 1 as this {@code BigDecimal} is numerically
// less than, equal to, or greater than {@code val}.
GT(1), EQ(0), LT(-1);
private final Integer value;
}
public static boolean eq(BigDecimal a, BigDecimal b) {
return (a == b) || (a != null && a.equals(b));
}
public static boolean neq(BigDecimal a, BigDecimal b) {
return !eq(a, b);
}
public static boolean gt(BigDecimal a, BigDecimal b) {
return (a != null && b == null) || (a != null && b != null && CompareResultEnum.GT.value.equals(a.compareTo(b)));
}
public static boolean gte(BigDecimal a, BigDecimal b) {
return eq(a, b) || gt(a, b);
}
public static boolean lt(BigDecimal a, BigDecimal b) {
return (a == null && b != null) || (a != null && b != null && CompareResultEnum.LT.value.equals(a.compareTo(b)));
}
public static boolean lte(BigDecimal a, BigDecimal b) {
return eq(a, b) || lt(a, b);
}
}
分摊工具
package com.swy.math;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import lombok.Data;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.function.Function;
public class ShareUtils {
public static void share(Integer num, List l, Function getKey
, Function getVal
, BiConsumer setVal){
// 获取总数
Integer ratioTotalQty = l.stream().mapToInt(o -> getKey.apply(o)).sum();
if (ratioTotalQty <= 0) {
return;
}
int scale = 9;
// 第一次分摊的数量
int firstResultTotal = 0;
Map rawMap = new HashMap<>(l.size());
Map firstResultMap = new HashMap<>(l.size());
for (int i = 0; i < l.size(); i++) {
T t = l.get(i);
Integer ratioQty = getKey.apply(t);
Double ratio = new BigDecimal((float) ratioQty/ratioTotalQty).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入
double firstNum = new BigDecimal(ratio * num).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();// 四舍五入;
int firstResult = new BigDecimal(ratio * num).setScale(0, BigDecimal.ROUND_DOWN).intValue();// 直接舍弃小数
double secondNum = new BigDecimal(firstNum - firstResult).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
rawMap.put(i, secondNum);
firstResultMap.put(i, firstResult);
firstResultTotal = firstResultTotal + firstResult;
}
// 第一次分摊后剩余的数量; 按第一次分摊剩余小数进行倒叙排列. 排序结果和总差对比 ,如果小于等于总差,则为1,大于总差则为0,用于补回总差
int waitAllocateNum = num - firstResultTotal;
Map sortRawMap = sortByValue(rawMap, true);
AtomicInteger atomicIndex = new AtomicInteger(1);
int size = sortRawMap.size();
if(waitAllocateNum > size){
throw new RuntimeException("分摊工具类异常,剩余分摊数量:"+waitAllocateNum+",分摊拥有者数:"+size);
}
sortRawMap.forEach((k, v)->{
T t = l.get(k);
int index = atomicIndex.getAndIncrement();
Integer firstResult = firstResultMap.getOrDefault(k,0);
int curAllocateNum = (index <= waitAllocateNum ? 1 : 0);
setVal.accept(t, firstResult + curAllocateNum);
});
}
public static > Map sortByValue(Map map, boolean isDesc) {
Map result = Maps.newLinkedHashMap();
if (isDesc) {
map.entrySet().stream().sorted(Map.Entry.comparingByValue().reversed())
.forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
} else {
map.entrySet().stream().sorted(Map.Entry.comparingByValue())
.forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
}
return result;
}
}
Demo
public class ShareTest {
public static void main(String[] args) {
List list = new ArrayList<>();
SwyShareBO shareBO1 = new SwyShareBO();
SwyShareBO shareBO2 = new SwyShareBO();
SwyShareBO shareBO3 = new SwyShareBO();
shareBO1.setKey(1);
shareBO2.setKey(2);
shareBO3.setKey(3);
list.add(shareBO1);
list.add(shareBO2);
list.add(shareBO3);
ShareUtils.share(294, list, SwyShareBO::getKey, SwyShareBO::getCount, SwyShareBO::setCount);
}
}



