gpt4 book ai didi

java - 复杂和不同数据结构的通用方法

转载 作者:行者123 更新时间:2023-12-02 13:18:21 29 4
gpt4 key购买 nike

我想为复杂结构创建一个通用方法,但我不知道如何将该方法的签名与我的数据结构相匹配。

我有以下结构:

我正在使用的一个类:

public class Individual <T extends AnInterface> {

final List<T> data;

public Individual(final List<T> data){
this.data = data;
}
public List<T> getData() {
return data;
}
}

我正在使用的较小结构:

public class Tuple3<T1,T2,T3> {

public T1 _1;
public T2 _2;
public T3 _3;

public Tuple3(T1 t1, T2 t2, T3 t3){
_1 = t1;
_2 = t2;
_3 = t3;
}

}

我想在其中使用泛型方法的泛型类:

class MyTemplatedClass <..., T2 extends AnInterface, ...> 
implements ... {

private Map<Integer, List<Tuple3<Integer, Individual<T2>, Double>>> resultsByIteration;
private Map<Individual<T2>, List<Tuple3<Integer, Individual<T2>, Double>>> resultsByIndividual;
private int iteration = 0;

@Override
public <T2 extends AnInterface> void evaluatePopulationAndStoreResults(List<Individual<T2>> elements) {

// changing iteration
// calculating fitness
for (Individual<T2> element : elements) {

Tuple3<Integer, Individual<T2>, Double> triplet = new Tuple3<>(iteration, element, fitness);

/// I want this operation to be templated for both strutures
doSomeListOperationOnMapForAGivenKey(resultsByIteration, new Integer(5), triplet);
doSomeListOperationOnMapForAGivenKey(resultsByIndividual, elements, triplet);
}
}

/// How should doSomeListOperationOnMapForAGivenKey() method's signature look like, if I want to call this on both data structures???
// this signature does not match to the above calls :(
public <KEY_TYPE, VALUE_TYPE> void doSomeListOperationOnMapForAGivenKey(Map<KEY_TYPE, List<VALUE_TYPE>> map, KEY_TYPE key, VALUE_TYPE value){
// some list operation:
// check if key is in map
// if not, then create a list, add the value to the list and put the key and value into the map
// if yes, then check if is there any similar tuple in the list under the key
// it there is no similar tuple, then add the value to the list
// if there is a similar tuple, then update with the value
}

}

我收到此签名的编译错误:

 public <KEY_TYPE, VALUE_TYPE> void doSomeListOperationOnMapForAGivenKey(Map<KEY_TYPE, List<VALUE_TYPE>> map, KEY_TYPE key, VALUE_TYPE value)

如果我想对不同类型的两个结构执行相同的映射和列表操作,我无法弄清楚 doSomeListOperationOnMapForAGivenKey() 方法的签名应该是什么样子。我希望这个方法尽可能通用。有什么想法吗?

更新:感谢 Radiodef 的评论,修复了类定义中的错误,现在看起来像这样:

class MyTemplatedClass <..., T2 extends AnInterface, ...> 
implements SomeOtherInterface<T2> {...}

调用者方法的签名如下:

@Override
public void evaluatePopulationAndStoreResults(List<Individual<T2>> elements) {...}

最佳答案

编译器错误试图告诉您的是,在类上声明的 T2 和在方法 evaluatePopulationAndStoreResults 上声明的 T2 引用了不同类型。

class MyTemplatedClass <..., T2 extends AnInterface, ...> 
implements ... {

@Override
// Declares a new type variable T2, unrelated to the
// T2 declared on the class and shadows it.
// vvvvvvvvvvvvvvvvvvvvvvvv
public <T2 extends AnInterface> void evaluatePopulationAndStoreResults(...) {
{

Tuple3<Integer, Individual<T2>, Double> triplet =
new Tuple3<>(iteration, element, fitness);

doSomeListOperationOnMapForAGivenKey(resultsByIteration,
new Integer(5),
triplet);
}
}

实际发生的事情是这样的:

// One type variable declared on the class, used by
// both maps.
// vvvvvvvvvvvvvvvvvvvvvvv
class MyTemplatedClass <..., T2A extends AnInterface, ...>
implements ... {

@Override
// Another different type variable declared
// on the method.
// vvvvvvvvvvvvvvvvvvvvvvvv
public <T2B extends AnInterface> void evaluatePopulationAndStoreResults(...) {
{

Tuple3<Integer, Individual<T2B>, Double> triplet =
new Tuple3<>(iteration, element, fitness);

// Passing a Map using T2A
// vvvvvvvvvvvvvvvvvv
doSomeListOperationOnMapForAGivenKey(resultsByIteration,
new Integer(5),
// Passing a Tuple3<..., T2B, ...>
// vvvvvvv
triplet);
}
}

但是在您的代码中,两个类型变量具有相同的名称。这与声明与实例成员同名的局部变量时发生的情况类似。

据我所知,您只需删除 evaluatePopulationAndStoreResults 上的类型变量声明即可。当您在类上声明类型变量时,整个类都可以访问它。

我认为如果删除隐藏类型变量声明,所示的代码片段应该可以编译。

关于java - 复杂和不同数据结构的通用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43681964/

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