简体   繁体   English

Hashtable Java中Long的问题

[英]Issues with Long in Hashtable Java

Apparently, I am unable to store a Long value in a hashtable. 显然,我无法在哈希表中存储Long值。

See the code below: 请参见下面的代码:

//create a hashtable of type <String, Long>
Hashtable <String, Long> universalTable = new Hashtable <String, Long> ();

universalTable.put("HEADS", new Long(0)); // this works fine

I pass this table in the constructor for DoFlip : 我将此表传递给DoFlip的构造DoFlip

DoFlip doFlip = new DoFlip(100000000, universalTable);

Inside DoFlip : 内部DoFlip

Hashtable table; // pointer to hash map
long iterations = 0; // number of iterations

DoFlip(long iterations, Hashtable table){
    this.iterations = iterations;
    this.table = table;
}

This class implements Runnable. 此类实现Runnable。 The run() method is as follows— run()方法如下:

public void run(){
    while(this.iterations > 0){
        // do some stuff
        this.heads ++;
        this.iterations --;
    }
    updateStats();
}

public void updateStats(){
    Long nHeads = (Long)this.table.get("HEADS");
    this.table.put("HEADS", nHeads); // ISSUE HERE
}

I get the following warning/error. 我收到以下警告/错误。 Looks like a warning but I don't want this. 看起来像是警告,但我不想要这个。

Note: File.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

When I recompile: 当我重新编译时:

File.java:92: warning: [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.Hashtable
            this.table.put("HEADS", nHeads);
                          ^
1 warning

I am unsure why this is the case. 我不确定为什么会这样。 Firstly, there isn't any need to type cast nHeads . 首先,不需要键入nHeads But I still do it and it doesn't work. 但是我仍然这样做,并且不起作用。

Note: I am not good in Java at all. 注意:我一点都不擅长Java。 :/ :/

Thanks for the help. 谢谢您的帮助。

This warning indicates that you are using a raw type. 此警告表明您正在使用原始类型。 Replace 更换

DoFlip(long iterations, Hashtable table){

with

DoFlip(long iterations, Hashtable<String, Long> table) {

such that it contains the generics similar to universalTable . 这样它就包含类似于universalTable的泛型。 Also include the generics in the initial declaration. 还应在初始声明中包含泛型。

Side note: 边注:

  • Hashtable is a pretty old Collection and has been replaced by HashMap . Hashtable是一个相当老的Collection ,已由HashMap取代。

This is just a warning, telling you that you are mixing generic and non-generic containers. 这只是一个警告,告诉您正在混合通用容器和非通用容器。 This is allowed, but the compiler can do a better job at type checking if you use generics everywhere in your code. 这是允许的,但是如果您在代码的任何地方都使用泛型,则编译器可以在类型检查方面做得更好。

To fix this warning, you need to change 要解决此警告,您需要进行更改

Hashtable table;

for 对于

Hashtable<String, Long> table;

in declarations inside DoFlip . DoFlip内的DoFlip

My 2 cents: 我的2美分:

Firstly, if you are building some performance sensitive application and you want to avoid the conversion between Long and long primitive, consider using trove4j collection library. 首先,如果要构建一些对性能敏感的应用程序,并且希望避免在Long和long原语之间进行转换,请考虑使用trove4j集合库。 It's a primitive based one with good quality. 它是基于原始的,质量很高。

Secondly, your DoFlip should be declared as 其次,您的DoFlip应该声明为

DoFlip(long iterations, Hashtable<String, Long> table){
    this.iterations = iterations;
    this.table = table;
}

and problem solved. 问题解决了。

Enjoy. 请享用。

You need to reassure the compiler that your HashMaps are all from Strings to Longs. 您需要向编译器保证,您的HashMap都是从Strings到Longs。 You did it here: 您是在这里完成的:

Hashtable <String, Long> universalTable = new Hashtable <String, Long> ();

... but not here: ...但不在这里:

Hashtable table; // pointer to hash map
---
DoFlip(long iterations, Hashtable table){

So do: 这样:

Hashtable<String, Long> table;
---
DoFlip(long iterations, Hashtable<String, Long> table){

...and there shall be no more automated panic that you'll put the wrong types of objects in table at runtime, since now the compiler can check that you always use the ones you intended (ie the ones specified in the brackets). ...并且不再会有自动恐慌,您会在运行时将错误类型的对象放入table中,因为现在编译器可以检查您始终使用所需的对象(即括号中指定的对象)。

This is just a warning from compiler that you are mixing generic and non-generic containers 这只是来自编译器的警告,提示您mixing generic and non-generic containers

You could do either of the following steps to make it disappear 您可以执行以下任一步骤使其消失

1)you need to change 1)您需要更改

Hashtable table;

for 对于

Hashtable<String, Long> table;

OR 要么

2)You can use SuppressWarning annotation to suppress the warning 2)您可以使用SuppressWarning注释来禁止显示警告

@SuppressWarnings("unchecked")
public void updateStats(){
    Long nHeads = (Long)this.table.get("HEADS");
    this.table.put("HEADS", nHeads); // ISSUE HERE
}

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

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