12.7 small improvement (java version)

#1

In the solution for streaming, if we instantiate the dict object by using accessOrder = true in the constructor, which means entries sort by access order, then we wouldn’t need the dict.remove(s) anymore right? We are already doing “Integer it = dict.get(s)” which means every time we encounter a word that is part of the query string set, we are enforcing an access from the LinkedHashMap. So the element is already at the front of the queue before we do dict.put.

Secondly, it’s better to move if (numStringsFromQueryStringsSeenSoFar == queryStrings.size()) inside if (dict.containsKey(s)) {
There’s no point of doing the check if the next word in the stream is not even in the hash map.

0 Likes

#2

Hi @ray,

Thanks for the suggestion, and would you mind to post the updated code here? I did not know the existence of accessOrder until you mentioned that.

0 Likes

#3

LinkedHashMap<String, Integer> dict = new LinkedHashMap<>(16, 1, true);

0 Likes

#4

@ray, thanks for this sample usage! I tried this and it helped us removing the use of dict.remove(s), and that is great!

0 Likes