1

I am trying to build in-memory cache (using a ConcurrentHashMap in Java 8).The key value pair would be a json string and the result of a complex operation on that string.

The objective is to not do the complex operation everytime and do it only when the json string changes.

Is there a way I can uniquely represent this string as the value of any of the json keys can change within the application at any time.

I have looked up the hashCode() method but saw the shortcomings of it.

Right now am trying to see if the MD5 representation of the string would serve as a good key for the JSON string.

If anyone has already faced such a situation, can you please provide your inputs?

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36

2 Answers2

2

As I understand it, a java String instance is final (immutable), so that even if the JSON object is a very long string, the String class only calculates the hashCode of the String once (at construction time or first use i can't remember) - and keeps it as an instance attribute for the lifetime of the String. So there is no problem (in terms of performance penalty) using the JSON object both as the key and value in a concurrent HashMap. This is exactly what the same as how a java "Set" works, being backed by a Map.

pjklauser
  • 1,156
  • 11
  • 13
  • Thanks pjklauser. The issue I am trying to solve is that I have a json string with a set of key value pairs which can change at any point of time in a day.This string is an input to a complex operation.I am trying not to perform that operation and delay it until a change in the JSON string has actually occured and use a hashmap to store the result of operation until data changes.So the question is,without doing any customization of overriding hashCode and equals method,is it possible to rely on hashCode generated and expect it to not change for a string until the string itself changes? – kartheek desineedi Jun 15 '16 at 22:32
  • A String never changes, you always construct new Strings which each have a fixed hashcode based on the string content. Two strings with the same content (start to finish same characters) will have the same hashcode and be considered 'equals'. The problem is that your idea is not robust because any change in the JSON data which is not relevant to your complex function (ie. the attributes ) will consider the string different - for example whitespace / pretty printing etc. which is irrelevant for the semantics of JSON but make different strings. – pjklauser Jun 16 '16 at 19:51
  • pjklauser the whitespace,etc are not going to be an issue as that is always the same since it is generated by the system,the only thing which change are the values inside the json.I am trying to do this transformation of the json string to a unique key in order to not have duplicates in my cache. – kartheek desineedi Jun 17 '16 at 10:41
0

This should be a easy task for Cacheonix, and you will save time on building your own caching solution:

Cache<String, ResultOfCalculation> cache = Cacheonix.getInstance().getCache("my.cache");
cache.put(myJsonString, myResultofCalcualtion);
...
ResultOfCalculation result = cache.get(myJsonString);
Slava Imeshev
  • 1,360
  • 10
  • 14