作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有些东西是有重量的,所以这是它们的界面,例如:
public interface Banana {
public Double getWeight();
}
// This function is a static function in a public tool class.
@Nullable
public static Banana pickItemByTheirWeight(List<Banana> originalVideos) {
// ...
return randomOneByWeight;
}
现在我想创造有重量的东西,所以:
class Video implements Banana {
@Override
public Double getWeight() {
//....
return weight;
}
}
现在,当我想按权重选择视频时,例如:
List<Video> videos = new ArrayList<>();
// ...
Video randomOne = pickItemByTheirWeight(videos);
我收到编译错误。那么正确的做法是什么呢?
<小时/>嘿伙计们,这并不容易。
我试过了
public static <T extends Banana> T pickOneByTheirWeight(List<T> domains) {
// ...
}
public abstract class Banana {
public abstract Double getWeight();
}
它仍然不起作用,我无法在不转换类类型的情况下调用该函数。
* 有人能告诉我为什么 Java 选择不让它正常工作吗???*
<小时/>阅读了一些Java泛型的基础知识后,这里有一个解决方案:
public static <T extends Banana> T pickOneByTheirWeight(List<? extends Banana> domains) {
if (domains.isEmpty()) {
return null;
}
// calculate total weight
Double totalWeight = domains.stream().map(x -> {
Double weight = x.getWeight();
return weight == null ? 0.0 : weight;
}).reduce((first, second) -> {
Double firstWeight = first != null ? first : 0.0;
Double secondWeight = second != null ? second : 0.0;
return firstWeight + secondWeight;
}).get();
// random process
final Double randomSeed = Math.random() * totalWeight;
double currentWeight = 0.0;
for (Banana v: domains) {
Double weight = v.getWeight();
weight = weight == null ? 0.0 : weight;
currentWeight += weight;
if (currentWeight >= randomSeed) {
return (T) v;
}
}
// it'll not reach here indeed.
logger.warn("pickDomainByTheirWeight cannot pick an element from list by their weights.");
return null;
}
但是像这样声明函数,我们可以简单地调用:
List<Video> videos = new ArrayList<>();
// ...
Video p = Utility.pickOneByTheirWeight(videos);
但是,不再在外部进行强制转换,仍然在函数内部进行强制转换。还有更好的主意吗?
最佳答案
因为List<Banana>
不是List<Video>
。将方法签名更改为:
@Nullable
public static Banana pickItemByTheirWeight(List<Video> originalVideos) {
// ...
return randomOneByWeight;
}
或者更好 - 使用通用父类(super class):
@Nullable
public static Banana pickItemByTheirWeight(List<? extends Banana> originalVideos) {
// ...
return randomOneByWeight;
}
关于java - 如何使用带有 List 参数的泛型函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57720293/
我是一名优秀的程序员,十分优秀!