java 当一个可以为 null 时,找到来自两个不同对象类型的两个数字中的最小值
java find the smallest of two numbers coming from two different object types when one can be null
当数字是两个不同对象内部的属性时,我正在尝试找出获取两个数字中最小值的最佳方法。每个对象(但不是两个)都可以为 null,这会导致空指针异常。每个对象都有自己的 getValue() 方法,它将 return 一个 Long 值。有一些我不想做的基本 if/else:
if (obj1 != null && obj2 != null) { // neither object is null
minValue = obj1.getValue() <= obj2.getValue() ? obj1.getValue() : obj2.getValue();
} else if (obj1 == null && obj2 != null) { // only obj1 is null
minValue = obj2.getValue();
} else { // only obj2 is null (they can't both be null, so we don't need an if for the situation where they're both null)
minValue = obj1.getValue();
}
我尝试了一些其他的东西:
// can throw null pointer exception
Collections.min(Arrays.asList(obj1.getValue(), obj2.getValue()));
// while both objects have a getValue() method, they are of different types, so mapping doesn't work
Collections.min(Arrays.asList(obj1, obj2)
.filter(obj -> obj != null)
.map(obj -> obj.getValue()) // this line will fail since the methods correspond to different objects
.collect(Collectors.toList()));
我觉得这应该是一个相当简单的问题,但我的大脑不允许它工作。由于有一些 min 函数,您可以在其中 bipass 对象可以为 null 的情况?
我不确定我是否完全理解你的问题,但如果我理解了,这样的事情可能会奏效:
if(obj1 == null)
minValue = obj2.getValue();
else if(obj2 == null)
minValue = obj1.getValue();
else minValue = obj1.getValue() < obj2.getValue() ? obj1.getValue() : obj2.getValue();
long minValue = Math.min(obj1 == null ? Long.MAX_VALUE : obj1.getValue(),
obj2 == null ? Long.MAX_VALUE : obj2.getValue());
你可以有一个方法接受你的 ObjType,做空检查和 returns Long.MAX_VALUE,或者如果它是空的值。
public Long getVal(ObjType val)
{
if(val != null)
{
return val.getValue();
}
return Long.MAX_VALUE;
}
然后
Math.min(obj1, obj2);
当数字是两个不同对象内部的属性时,我正在尝试找出获取两个数字中最小值的最佳方法。每个对象(但不是两个)都可以为 null,这会导致空指针异常。每个对象都有自己的 getValue() 方法,它将 return 一个 Long 值。有一些我不想做的基本 if/else:
if (obj1 != null && obj2 != null) { // neither object is null
minValue = obj1.getValue() <= obj2.getValue() ? obj1.getValue() : obj2.getValue();
} else if (obj1 == null && obj2 != null) { // only obj1 is null
minValue = obj2.getValue();
} else { // only obj2 is null (they can't both be null, so we don't need an if for the situation where they're both null)
minValue = obj1.getValue();
}
我尝试了一些其他的东西:
// can throw null pointer exception
Collections.min(Arrays.asList(obj1.getValue(), obj2.getValue()));
// while both objects have a getValue() method, they are of different types, so mapping doesn't work
Collections.min(Arrays.asList(obj1, obj2)
.filter(obj -> obj != null)
.map(obj -> obj.getValue()) // this line will fail since the methods correspond to different objects
.collect(Collectors.toList()));
我觉得这应该是一个相当简单的问题,但我的大脑不允许它工作。由于有一些 min 函数,您可以在其中 bipass 对象可以为 null 的情况?
我不确定我是否完全理解你的问题,但如果我理解了,这样的事情可能会奏效:
if(obj1 == null)
minValue = obj2.getValue();
else if(obj2 == null)
minValue = obj1.getValue();
else minValue = obj1.getValue() < obj2.getValue() ? obj1.getValue() : obj2.getValue();
long minValue = Math.min(obj1 == null ? Long.MAX_VALUE : obj1.getValue(),
obj2 == null ? Long.MAX_VALUE : obj2.getValue());
你可以有一个方法接受你的 ObjType,做空检查和 returns Long.MAX_VALUE,或者如果它是空的值。
public Long getVal(ObjType val)
{
if(val != null)
{
return val.getValue();
}
return Long.MAX_VALUE;
}
然后
Math.min(obj1, obj2);