Reputation: 4820
Apparently, I am unable to store a Long value in a hashtable.
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 = new DoFlip(100000000, universalTable);
Inside 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. The run()
method is as follows—
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
. But I still do it and it doesn't work.
Note: I am not good in Java at all. :/
Thanks for the help.
Upvotes: 0
Views: 2259
Reputation: 159844
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
. Also include the generics in the initial declaration.
Side note:
Hashtable
is a pretty old Collection
and has been replaced by HashMap
.Upvotes: 3
Reputation: 4873
This is just a warning from compiler that you are mixing generic and non-generic containers
You could do either of the following steps to make it disappear
1)you need to change
Hashtable table;
for
Hashtable<String, Long> table;
OR
2)You can use SuppressWarning annotation to suppress the warning
@SuppressWarnings("unchecked")
public void updateStats(){
Long nHeads = (Long)this.table.get("HEADS");
this.table.put("HEADS", nHeads); // ISSUE HERE
}
Upvotes: 0
Reputation: 3129
My 2 cents:
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. It's a primitive based one with good quality.
Secondly, your DoFlip should be declared as
DoFlip(long iterations, Hashtable<String, Long> table){
this.iterations = iterations;
this.table = table;
}
and problem solved.
Enjoy.
Upvotes: 1
Reputation: 1519
You need to reassure the compiler that your HashMaps are all from Strings to 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 (i.e. the ones specified in the brackets).
Upvotes: 0
Reputation: 726809
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
.
Upvotes: 3