Best way to save two depending Strings in Java and compare if new strings already exist -
Best way to save two depending Strings in Java and compare if new strings already exist -
i need save 2 depending strings (action , parameter) file or hashtable/map or array, depending best solution speed , memory.
my application iterates through big amount of forms on website , want skip if combination (string action,string parameter) tested , hence saved. thing array slow if have more thousands of different action , parameter tupels. i´m not experienced plenty chose right method this. tried hashtable not work:
hashtable<string, string> ht = new hashtable<string, string>(); if (ht.containskey(action) && ht.get(action).contains(parameter)) { system.out.println("tupel exists"); continue; } else ht.put(action, parameter);
if action , parameter 1-to-1 mapping (an action ever have 1 parameter), basic premise should fine (though i'd recommend hashmap on hashtable it's faster , supports null keys)
if have many parameters given action, want map<string, set<string>>
- action key , each action associated set of parameters.
declare this:
map<string, set<string>> map = new hashmap<>();
use this:
set<string> parameterset = map.get(action); // lookup parameterset if ((parameterset != null) && (parameterset.contains(parameter)) { // if exists , contains key system.out.println("tupel exists"); } else { // pair doesn't exist if (parameterset == null) { // create parameterset if needed parameterset = new hashset<string>(); map.put(action, parameterset); } parameterset.add(parameter); // , add together parameter }
as rest of code , other things may not working:
i'm not sure utilize ofcontinue
in original code; it's hard tell without rest of method. i'm assuming creation of hashtable separated usage - if you're recreating each time, you'll have problems. java string
Comments
Post a Comment