gpt4 book ai didi

java - Java 类型系统中的错误?

转载 作者:搜寻专家 更新时间:2023-11-01 03:10:16 25 4
gpt4 key购买 nike

假设有一个主类 -- Simulator -- 使用另外两个类 -- ProducerEvaluators,它们实现接口(interface) IProducer 和IEvaluator,分别。

IProducer 实现产生结果,而 IEvaluator 实现评估这些结果。模拟器通过查询 IProducer 实现来控制执行流程,然后将结果传送给 IEvaluator 实例。

Producer 和 Evaluator 的实际实现在运行时是已知的,在编译时我只知道它们的接口(interface)。检查下面的例子。

package com.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
* Producers produce results. I do not care what their actual type is, but the
* values in the map have to be comparable amongst themselves.
*/
interface IProducer<T extends Comparable<T>> {
public Map<Integer, T> getResults();
}

/**
* This example implementation ranks items in the map by using Strings.
*/
class ProducerA implements IProducer<String> {
@Override
public Map<Integer, String> getResults() {
Map<Integer, String> result = new HashMap<Integer, String>();
result.put(1, "A");
result.put(2, "B");
result.put(3, "B");

return result;
}
}

/**
* This example implementation ranks items in the map by using integers.
*/
class ProducerB implements IProducer<Integer> {
@Override
public Map<Integer, Integer> getResults() {
Map<Integer, Integer> result = new HashMap<Integer, Integer>();
result.put(1, 10);
result.put(2, 30);
result.put(3, 30);

return result;
}
}

/**
* Evaluator evaluates the results against the given groundTruth. All it needs
* to know about results, is that they are comparable amongst themselves.
*/
interface IEvaluator {
public <T extends Comparable<T>> double evaluate(Map<Integer, T> results,
Map<Integer, Double> groundTruth);
}

/**
* This is example of an evaluator, metric Kendall Tau-B. Don't bother with
* semantics, all that matters is that I want to be able to call
* r1.compareTo(r2) for every (r1, r2) that appear in Map<Integer, T> results.
*/
class KendallTauB implements IEvaluator {
@Override
public <T extends Comparable<T>> double evaluate(Map<Integer, T> results,
Map<Integer, Double> groundTruth) {
int concordant = 0, discordant = 0, tiedRanks = 0, tiedCapabilities = 0;

for (Entry<Integer, T> rank1 : results.entrySet()) {
for (Entry<Integer, T> rank2 : results.entrySet()) {
if (rank1.getKey() < rank2.getKey()) {
final T r1 = rank1.getValue();
final T r2 = rank2.getValue();
final Double c1 = groundTruth.get(rank1.getKey());
final Double c2 = groundTruth.get(rank2.getKey());

final int ranksDiff = r1.compareTo(r2);
final int actualDiff = c1.compareTo(c2);

if (ranksDiff * actualDiff > 0) {
concordant++;
} else if (ranksDiff * actualDiff < 0) {
discordant++;
} else {
if (ranksDiff == 0)
tiedRanks++;

if (actualDiff == 0)
tiedCapabilities++;
}
}
}
}

final double n = results.size() * (results.size() - 1d) / 2d;

return (concordant - discordant)
/ Math.sqrt((n - tiedRanks) * (n - tiedCapabilities));
}
}

/**
* The simulator class that queries the producer and them conveys results to the
* evaluator.
*/
public class Simulator {
public static void main(String[] args) {
// example of a ground truth
Map<Integer, Double> groundTruth = new HashMap<Integer, Double>();
groundTruth.put(1, 1d);
groundTruth.put(2, 2d);
groundTruth.put(3, 3d);

// dynamically load producers
List<IProducer<?>> producerImplementations = lookUpProducers();

// dynamically load evaluators
List<IEvaluator> evaluatorImplementations = lookUpEvaluators();

// pick a producer
IProducer<?> producer = producerImplementations.get(0);

// pick an evaluator
IEvaluator evaluator = evaluatorImplementations.get(0);

// evaluate the result against the ground truth
double score = evaluator.evaluate(producer.getResults(), groundTruth);

System.out.printf("Score is %.2f\n", score);
}

// Methods below are for demonstration purposes only. I'm actually using
// ServiceLoader.load(Clazz) to dynamically discover and load classes that
// implement interfaces IProducer and IEvaluator
public static List<IProducer<?>> lookUpProducers() {
List<IProducer<?>> producers = new ArrayList<IProducer<?>>();
producers.add(new ProducerA());
producers.add(new ProducerB());

return producers;
}

public static List<IEvaluator> lookUpEvaluators() {
List<IEvaluator> evaluators = new ArrayList<IEvaluator>();
evaluators.add(new KendallTauB());

return evaluators;
}
}

此代码在没有警告的情况下编译并且也按应有的方式运行。这是 solution to the question that I asked before ,所以这是一个后续问题。

使用上面的代码,假设您想将 producer.getResults() 调用的结果存储在一个变量中(稍后将在对 evaluator.evaluate(results, groundTruth) 调用的调用中使用) . 该变量的类型是什么?

map <整数, ?>, map <整数, ?扩展比较 >?使主要方法通用并使用通用类型?到目前为止,我尝试过的任何方法都不起作用。编译器会提示我提出的每种类型。

public static void main(String[] args) {
// example of a ground truth
Map<Integer, Double> groundTruth = new HashMap<Integer, Double>();
groundTruth.put(1, 1d);
groundTruth.put(2, 2d);
groundTruth.put(3, 3d);

// dynamically load producers
List<IProducer<?>> producerImplementations = lookUpProducers();

// dynamically load evaluators
List<IEvaluator> evaluatorImplementations = lookUpEvaluators();

// pick a producer
IProducer<?> producer = producerImplementations.get(0);

// pick an evaluator
IEvaluator evaluator = evaluatorImplementations.get(0);

// evaluate the result against the ground truth
Map<Integer, ?> data = producer.getResults(); // this type works
double score = evaluator.evaluate(data, groundTruth); // but now this call does not


System.out.printf("Score is %.2f\n", score);
}

似乎 producer.getResults() 返回了一些不能用 Java 静态表达的东西。这是一个错误,还是我遗漏了什么?

最佳答案

我回答之前的一个注释:你所有的T extends Comparable<T>语句可能应该是 T extends Comparable<? super T> ,它允许更大的灵 active (你为什么要关心比较 T s 或 Object s?),并且它是我的解决方案工作所必需的。

这并不是 Java 类型系统的真正“错误”,只是它带来的不便。 Java 并不特别喜欢将交集类型作为类型声明的一部分。

我发现解决这个问题的一种方法是创建一个在正常情况下永远不应该使用的“不安全”方法:

@SuppressWarnings("unchecked")
private static <T extends Comparable<? super T>> Map<Integer, T> cast(Map<Integer, ?> map) {
return (Map<Integer, T>) map;
}

只需确保使用 Map 调用此方法即可那实际上是一个 Map<Integer, T extends Comparable<? super T>> (就像那些 IProducer 的返回)。

使用此方法,您可以执行以下操作:

IProducer<?> producer = ...
IEvaluator evaluator = ...
Map<Integer, ?> product = producer.getResults();
evaluator.evaluate(cast(product), truth);

然后 Java 会自动为您推断出正确的类型参数。

另外,I前缀是 generally frowned upon在 Java 社区。

关于java - Java 类型系统中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12254897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com