participate


Collections: Lists, Sets, and Maps - Question about Hashtable
<<   Back to Forum  |   Give us Feedback
This topic has 8 replies on 1 page.
outlook__express
Posts:1
Registered: 1/26/05
Question about Hashtable   
Jan 26, 2005 1:02 AM

 
hi
i have a problem about Hashtable here,when i compile the following code, a warning prompt that "unchecked call to put(K, V) as a member of the raw type java.util.Hashtable",would you please help me?

code:
...
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "testFactory");
...

btw: what does the the point brackets mean in the following code?
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, java.io.Serializable {
...
 
philip_ross
Posts:407
Registered: 11/10/98
Re: Question about Hashtable   
Jan 26, 2005 1:37 AM (reply 1 of 8)  (In reply to original post )

 
The warning relates to the new generics feature of Java 5.0. The K and V refer to two paremeterized types - one for the key, the other for the value. When using a generic type, you can instantiate these parameters with real types. For example to create a Hashtable that maps String objects to Widget objects you could do the following:

Hashtable<String,Widget> h = new Hashtable<String,Widget>();
h.add("test", new Widget("test"));
Widget w = h.get("test");  // note: no cast needed here


Your code could be changed to the following:

Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "testFactory");


See http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf for Sun's generics tutorial.

To just switch off the warnings, you can use the "-Xlint:-unchecked" option of javac (see http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javac.html).

To switch of generics completely, you can tell javac to treat the code as 1.4 compliant with the "-source 1.4" option.
 
Peter-Lawrey
Posts:6,487
Registered: 5/5/04
Re: Question about Hashtable   
Jan 26, 2005 2:47 AM (reply 2 of 8)  (In reply to #1 )

 
technically the old Hashtable is equivilent to the new Hashtable<Object, Object>
 
robbix.the.bobbix
Posts:61
Registered: 10/17/04
Re: Question about Hashtable   
Jan 26, 2005 4:50 AM (reply 3 of 8)  (In reply to #2 )

 
Many classes in the API have done just that so that they do not have to change the rest of their code. The class Properties used to extend java.util.Hashtable and cast objects (strings) that were being used as property keys and values. Now, they just extended java.util.Hashtable<Object, Object> and kept the type-checking, becuase you you try to save a list of properties with Objects in them, you get Cast exceptions. If the designers has extended java.util.Hashtable<String, String> then this wouldn't be a problem, but I guess they were just being lazy. Feel free to be lazy yourself when recompiling old code, but it would be easier to take advantage of generic types for anything new that your write.
 
furiousp
Posts:7
Registered: 2/15/05
Re: Question about Hashtable   
Feb 15, 2005 12:07 PM (reply 4 of 8)  (In reply to #3 )

 
Sorry for bringing back an old thread but i'm having the same problem with hashtables. How come i can't use something like this:

static Hashtable<byte, ByteList> moves = new Hashtable<byte, ByteList>();

(ByteList is my own class)

everywhere i look everyone suggests using Hashtable<String, String> but what if i don't want to use a string as the key?
 
nasch_
Posts:6,747
Registered: 1/25/04
Re: Question about Hashtable   
Feb 15, 2005 12:58 PM (reply 5 of 8)  (In reply to #4 )

 
How come i can't use something like this:

static Hashtable<byte, ByteList> moves = new Hashtable<byte, ByteList>();

I give up, why?

What I mean by that is, what is stopping you from doing that?
 
Herko_ter_Horst
Posts:2,362
Registered: 8/28/03
Re: Question about Hashtable   
Feb 16, 2005 1:18 AM (reply 6 of 8)  (In reply to #5 )

 
You can't use primitives as keys (or values) in the Collections framework. However, you can make use of autoboxing to achieve the same result with little or no hassle:
		Map<Byte, Object> myMap = new HashMap<Byte, Object>();
		
byte key = 1;
Object value = "Hello World";
 
// put test
myMap.put(key, value);
		
// get test
Object getValue = myMap.get(key);
 
furiousp
Posts:7
Registered: 2/15/05
Re: Question about Hashtable   
Feb 16, 2005 2:56 AM (reply 7 of 8)  (In reply to #6 )

 
I've tried this and am still getting the same problem.

My code looks like this:

static Hashtable<Byte, ByteList> moves = new Hashtable<Byte, ByteList>();
....
moves.put(count, new ByteList(scMovesArray, true));

I've also tried:

static Hashtable<Byte, Object> moves = new Hashtable<Byte, Object>();

I suppose i could just turn the bloody things off but this is a project i'm doing so i should probably do things properly
 
Herko_ter_Horst
Posts:2,362
Registered: 8/28/03
Re: Question about Hashtable   
Feb 17, 2005 1:57 AM (reply 8 of 8)  (In reply to #7 )

 
What "same problem" are you still getting? Compiler warning? On what line? Are you sure the problem is not in another part of your code?

Is 'count' indeed a byte? If count is just a simple incrementing count, why not just use a List instead of a Map?

Also, I would recommend the use of a HashMap over a Hashtable.
 
This topic has 8 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 133

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums