【发布时间】:2018-04-28 21:10:11
【问题描述】:
我有一个类测试,其中 K、V、T - 键、值、时间戳 我有一个接口 put(K key, V value, T timestamp)
我的hashmap初始化如下:
import java.util.*;
import java.lang.*;
import java.io.*;
class Test1<K,V,T> {
private final HashMap<K, TreeMap<T, V>> map = new HashMap<K, TreeMap<T, V>>();
private void put(K key, V value, T timeStamp) {
}
public void put(K key, V value){
// Here it gives compiler error
put(key, value, System.currentTimeMillis());
}
}
/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Test1<Integer, Integer, Long> classInstance = new Test1<>();
classInstance.put(10, 10);
}
}
但是放在这里会给出编译器错误说明:
put(K, V, T) 不适用于参数 (Integer, Integer, Long)。
有人可以帮忙
【问题讨论】:
-
你的
put方法定义在哪里? -
代码对我来说工作正常
-
无法复制:ideone.com/Ora2qr
-
@OliverCharlesworth 我已经编辑帖子给你确切的错误。
-
T总是会是Long时间戳吗?如果是这样,那么您不需要第三个泛型参数。只需让类签名具有K和V并使用Long和V初始化TreeMap