views:

87

answers:

4

I'm using a 'Properties' class in Java which inherits from HashMap. I'm building a basic compiler in hashmap. I know it can be simplified using something besides a hashmap.

Here is the my code...

Properties inputSource = new Properties();
inputSource.put("ERROR", "ERROR");
inputSource.put("GET", "GET");
inputSource.put("PRINT", "PRINT");
inputSource.put("&", "&"); // this point input resource error.

I've tried to put "\&" instead, and it still didn't work. Is there any way I can add a & as a hashmap key?

+3  A: 

This should work. The error in your case might be in some other place than you are suspecting

this is what i tried -

Properties inputSource = new Properties();
inputSource.put("ERROR", "ERROR");
inputSource.put("PRINT", "PRINT");
inputSource.put("&", "&"); // this point input resource error.
inputSource.put("GET", "GET");

System.out.println(inputSource.get("&"));

Output

&
Gopi
+2  A: 

Your code is working perfectly fine for me..what is error message you are seeing? May be its your IDE showing an unnecessary warning..

(My answer should really be a comment..I got no other way..)

johnbk
+1 to boost your rep so that you comment :-)
chedine
@chedine - Love those precious points..Thanks!
johnbk
A: 

just ran your code under eclipse helios, jvm 1.4.2 it works perfectly

can you pase here the error you're getting?

savo.fra
A: 

For this particular code snippet, perhaps HashSet is more appropriate. Properties seems to do far more than you need. Of course, you may have some requirements that aren't clear from your question.

Bill Michell