My string looks like;
String values = "I am from UK, and you are from FR";
and my hashtable;
Hashtable countries = new Hashtable();
countries.put("United Kingdom", new String("UK"));
countries.put("France", new String("FR"));
What would be the most effective way to change the values in my string with the values from the hashtable accordingly. These are just 2 values to change, but in my case I will have 100+
I’m not sure there’s a whole lot you can do to optimize this in a way which is worthwhile. Actually you can construct an FSM for custom replacements like that but it’s probably more than you want to really do.
A couple of notes:
Don’t use
Hashtable. Use aMap(interface) andHashMap(class) instead;Declare your variable, parameter and return types, where applicable, as interfaces not concrete classes;
Assuming you’re using Java 5, use generic type arguments for more readable code. In this case,
Map<String, String>, etc; andDon’t use
new String("UK"). There is no need.