Reputation: 11693
package datastrcutures;
import java.util.*;
public class java_hashtable {
public static void ratingofcity() {
Hashtable CityRating = new Hashtable();
CityRating.put("New York", "8");
CityRating.put("Sandton", "9");
}
}
Upvotes: 0
Views: 204
Reputation: 1
import java.util.*;
public class java_hashtable {
public static void ratingofcity() {
Hashtable<String, String> cityRating = new Hashtable<String, String>();
CityRating.put("New York", "8");
CityRating.put("Sandton", "9");
}
}
Hashtable is declared in a wrong way. The changes which I made must work now.
Upvotes: 0
Reputation: 9652
The question Is this correct usage of a hashtable
is very subjective.
A Map
is used to storing sets of keys and values like in your example.
However - things you should consider in your design:
String
- wouldn't you prefer an Integer
so you can compare them?Also, a few notes not related to the "design" of this question:
Map<String, Integer> cityRatings = new Hashtable<String, Integer>();
Upvotes: 2
Reputation: 786261
I think you have a typo there, your object type has to be Hashtable
instead of Hasttable
And you should use Java Generics
Instantiate your hash table object like this:
Hashtable<String, String> cityRating = new Hashtable<String, String>();
And as Java naming convention I would suggest having your object name start with a lower case letter.
Upvotes: 2