简体   繁体   English

获取包含长值的哈希表上的方法

[英]Get method on hashtable that contains long values

I have a hashtable that I have declared as: Hashtable<Long, Long> sumhash = new Hashtable<Long, Long>();我有一个哈希表,我声明为: Hashtable<Long, Long> sumhash = new Hashtable<Long, Long>(); Now, when I check if there exists a key using:现在,当我使用以下命令检查是否存在密钥时:

if(sumhash.containsKey(diff) == true){
    start = sumhash.get(diff);
} //diff contains a Long value.

It works when I am checking if diff exists in the hashtable or not but when I am trying to get vaule using the get() method, It gives me error: incompatible types: Long cannot be converted to int它在我检查哈希表中是否diff时起作用,但是当我尝试使用get()方法获取值时,它给了我error: incompatible types: Long cannot be converted to int

I am new to Java, Thanks in advance.我是 Java 的新手,提前致谢。

You should cast it with.intValue().您应该使用 .intValue() 进行转换。 The issue here is that in the hash table you are storing the values as Long objects and thus you need a bit more than just parenthesis to cast.这里的问题是,在 hash 表中,您将值存储为 Long 对象,因此您需要的不仅仅是括号来转换。 Here is a more comprehensive explanation: https://www.javatpoint.com/java-long-to-int这里有更全面的解释: https://www.javatpoint.com/java-long-to-int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM