0

Suppose I have some class which does some operations with input string, but after executing all methods it should somehow store the resut for further inputs, so that if there'll be duplicate string just retrieve the result instead of running all methods again. How can I organize this result storing with minimum effort?

Johnny B. Goode
  • 121
  • 1
  • 10

1 Answers1

0

I suppose you could use a Map<String, String> to store your input String as the Key and the result as the value and just check if there exists a key in your Map with your given input String when calling your method. And if there exists a key just return the value, if not do your stuff.

If you need more persistence storage for example if your Application terminates and you still want to have the Map of all inputs and results then you would have to write to a file which is explained here and then read from it when your Application starts which you could look up here.

Paku580
  • 116
  • 5
  • And what about cache? Can I use it? Is there a built-in cahce in java? – Johnny B. Goode Oct 31 '19 at 09:47
  • Well the Map would be our cache more or less but there is already a discussion for real java in-memory caches you can check it out [here](https://stackoverflow.com/questions/575685/looking-for-simple-java-in-memory-cache) – Paku580 Oct 31 '19 at 16:38