0

I need to save two depending Strings (action and parameter) into a file or a hashtable/map or an Array, depending what is the best solution for speed and memory.

My Application iterates through a large amount of forms on a website and i want to skip if the combination (String action,String parameter) already was tested and therefore saved. I thing an Array would be too slow if I have more then thousands of different action and parameter tupels. I´m not experienced enough to chose the right method for this. I tried a hashtable but it does not work:

Hashtable<String, String> ht = new Hashtable<String, String>();
if (ht.containsKey(action) && ht.get(action).contains(parameter)) {
    System.out.println("Tupel already exists");
    continue;
}
else
    ht.put(action, parameter); 
msrd0
  • 7,816
  • 9
  • 47
  • 82
juzwani
  • 53
  • 2
  • 7
  • Hashtables can only have one element per key – msrd0 Nov 04 '14 at 17:47
  • Thx for the fast answer. So I won`t use hashtables what would be a good method? – juzwani Nov 04 '14 at 17:49
  • 3
    You could define a StringTuple class yourself, override equals and hashCode on this class and then store them in a HashSet. – Michael Krause Nov 04 '14 at 17:50
  • If you want to have only one parameter per action, use a Hashtable. Else use MichaelKrause's suggestion – msrd0 Nov 04 '14 at 17:53
  • I think @MichaelKrause suggestion is the best way to achieve that. Other way is to use HashMap>. With this approach you don't need additional tuple class. – Stanislav Lukyanov Nov 04 '14 at 18:00
  • 1
    Also note that Hashtable class is not a widely used one. Consider using HashMap instead. See more [here](http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable) – Stanislav Lukyanov Nov 04 '14 at 18:02
  • @StanislavLukyanov - totally agree; in fact, that's pretty much what my answer already says :) – Krease Nov 04 '14 at 18:05

1 Answers1

3

If a action and parameter will always be a 1-to-1 mapping (an action will only ever have one parameter), then your basic premise should be fine (though I'd recommend HashMap over Hashtable as it's faster and supports null keys)

If you will have many parameters for a given action, then you want Map<String, Set<String>> - where action is the key and each action is then associated with a set of parameters.

Declare it like this:

Map<String, Set<String>> map = new HashMap<>();

Use it like this:

Set<String> parameterSet = map.get(action);                          // lookup the parameterSet
if ((parameterSet != null) && (parameterSet.contains(parameter)) {   // if it exists and contains the key
    System.out.println("Tupel already exists");
} else {                                                             // pair doesn't exist
    if (parameterSet == null) {                                      // create parameterSet if needed
        parameterSet = new HashSet<String>();
        map.put(action, parameterSet);
    }
    parameterSet.add(parameter);                                     // and add your parameter
}

As for the rest of your code and other things that may not be working:

  • I'm not sure what your use of continue is for in your original code; it's hard to tell without the rest of the method.
  • I'm assuming the creation of your hashtable is separated from the usage - if you're recreating it each time, then you'll definitely have problems.
Stanislav Lukyanov
  • 2,147
  • 10
  • 20
Krease
  • 15,805
  • 8
  • 54
  • 86