Reputation: 596
I have existing code similar to this:
static Dictionary<String, Object> getDictionary() {
Dictionary props = new Properties();
Date date = new Date();
props.put(Constants.TIMESTAMP, date.getTime());
return props;
}
I'm just trying to make the Eclipse compiler happy and eliminate warnings the right way, without having to rewrite much of the code. AS expected, not using generic type parameters the compiler warns:
"Dictionary is a raw type. References to generic type Dictionary<K,V> should be parameterized."
The problem is that when I try to fix this, if I change Dictionary to Dictionary<String,Object> I make the return statement happy but the assignment:
Dictionary<String,Object> props = new Properties();
becomes invalid. That's because Properties is a Dictionary<Object,Object>. Now, if I try to change it to Dictionary<Object,Object> it will make the assignment work, but the return statement:
return props;
becomes invalid. So, How would you deal with a case like this? In general, how can I reuse a Properties object in a context like this, where I want to return a Dictionary<String,Object> to the caller?
Upvotes: 0
Views: 659
Reputation: 162
You could change the method to return static Dictionary<Object, Object> getDictionary()
.
Upvotes: 2
Reputation: 4574
Please do not use Dictionary
nor Hashtable
classes in new code. They are considered obsolete.
You can use Map, and for example HashMap as implementation. As per https://docs.oracle.com/javase/7/docs/api/java/util/Dictionary.html
If this is the code that has be like this (because you are integrating with some outdated API or somesuch), I just perform the necessary class casts, and put warning suppressions :(
Upvotes: 2
Reputation: 17535
You're using the wrong class. Properties is used as (but not defined as) a Dictionary<String, String> container. Use something like Hashtable<String, Object>.
Upvotes: -1