Java 8 如何在避免变异的同时避免返回空值?

Java 8 如何在避免变异的同时避免返回空值?,java-8,Java 8,我正在尝试创建一个方法,该方法将获取具有设置权重的项目列表,并随机选择1。我的解决方案是使用一个Hashmap,该Hashmap将整数作为权重,从Hashmap中随机选择一个键。HashMap的键可以是对象类型的混合,我想返回所选键中的1个 但是,我希望避免在避免变异的基础上返回null值。是的,我知道这是Java,但是有更优雅的方式来编写Java,并希望解决这个问题 public <T> T getRandomValue(HashMap<?, Integer> VALU

我正在尝试创建一个方法,该方法将获取具有设置权重的项目列表,并随机选择1。我的解决方案是使用一个Hashmap,该Hashmap将整数作为权重,从Hashmap中随机选择一个键。HashMap的键可以是对象类型的混合,我想返回所选键中的1个

但是,我希望避免在避免变异的基础上返回
null
值。是的,我知道这是Java,但是有更优雅的方式来编写Java,并希望解决这个问题

public <T> T getRandomValue(HashMap<?, Integer> VALUES) {
        final int SIZE = VALUES.values().stream().reduce(0, (a, b) -> a + b);
        final int RAND_SELECTION = ThreadLocalRandom.current().nextInt(SIZE) + 1;
        int currentWeightSum = 0;
        for (Map.Entry<?, Integer> entry : VALUES.entrySet()) {
            if (RAND_SELECTION > currentWeightSum && RAND_SELECTION <= (currentWeightSum + entry.getValue())) {
                return (T) entry.getKey();
            } else {
                currentWeightSum += entry.getValue();
            }
        }
        return null;
    }

public T getrandom值(HashMap由于循环后的代码在正常情况下永远不会到达,因此此时确实不应该编写类似于
returnnull
的内容,而是应该抛出异常,这样就可以在此时发现不规则的情况,而不是强迫调用方最终调试
NullPointerException
,可能发生在完全不同的地方

public static <T> T getRandomValue(Map<T, Integer> values) {
    if(values.isEmpty())
        throw new NoSuchElementException();
    final int totalSize = values.values().stream().mapToInt(Integer::intValue).sum();
    if(totalSize<=0)
        throw new IllegalArgumentException("sum of weights is "+totalSize);
    final int threshold = ThreadLocalRandom.current().nextInt(totalSize) + 1;
    int currentWeightSum = 0;
    for (Map.Entry<T, Integer> entry : values.entrySet()) {
        currentWeightSum += entry.getValue();
        if(threshold <= currentWeightSum) {
            return entry.getKey();
        }
    }
    // if we reach this point, the map's content must have been changed in-between
    throw new ConcurrentModificationException();
}
publicstatict getRandomValue(映射值){
if(values.isEmpty())
抛出新的NoTouchElementException();
final int totalSize=values.values().stream().mapToInt(Integer::intValue.sum();

如果(totalSize由于循环后的代码在正常情况下永远不会到达,因此此时确实不应该编写类似于
return null
的内容,而应该抛出异常,这样就可以在此时发现不规则的情况,而不是强迫调用方最终调试
NullPointerExcept离子
,可能发生在完全不同的地方

public static <T> T getRandomValue(Map<T, Integer> values) {
    if(values.isEmpty())
        throw new NoSuchElementException();
    final int totalSize = values.values().stream().mapToInt(Integer::intValue).sum();
    if(totalSize<=0)
        throw new IllegalArgumentException("sum of weights is "+totalSize);
    final int threshold = ThreadLocalRandom.current().nextInt(totalSize) + 1;
    int currentWeightSum = 0;
    for (Map.Entry<T, Integer> entry : values.entrySet()) {
        currentWeightSum += entry.getValue();
        if(threshold <= currentWeightSum) {
            return entry.getKey();
        }
    }
    // if we reach this point, the map's content must have been changed in-between
    throw new ConcurrentModificationException();
}
publicstatict getRandomValue(映射值){
if(values.isEmpty())
抛出新的NoTouchElementException();
final int totalSize=values.values().stream().mapToInt(Integer::intValue.sum();

如果(totalSizeFirst检查列表是否为空,并将结果包装为可选。因此,您返回了一个对象,可能带有一些值,并且很容易检查。但在Java中,返回空值并检查值+如果为空则作出反应是正常的,因此,如果列表为空,请考虑避免使用空值优先检查,并将结果包装为可选。因此,您获得了一个对象,并且可能有一些值,很容易检查。但是在Java中,返回null并检查value+如果null是正常的,那么可以考虑避免null