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");
}
}
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.
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>();
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.