0

I have a HashMap like this

 LinkedHashMap<K, V> = new LinkedHashMap<>(); 

this map is used as cache to implement LRU (least recently used)

In this cache I want to save results from functions that have no "side-effect" so for the same input they will always produce the same output.

Im also using Bi-Functions that have <T,U,R>

T and U are generic types. Here comes the Pair<T,U> into play. How do I save a Pair like this as a key into my Hash-Map?

And can someone explain how exactly the overwriting of the equals(Obj o) and hashCode() function is supposed to help with this problem?

real_G
  • 49
  • 8
  • What `Pair` class are you using and what have you tried up to now? And what gives you the idea that `equals` and `hashCode` play a role? They may, but if you don't know, it's kind of weird you're asking about them. – daniu Jan 19 '21 at 11:37
  • @daniu I read some other posts about using Pairs as a key in a hash-map and they said its important to implement ```equals``` and ```hashcode```. I wrote the Pair-class myself as Im using JDK15 – real_G Jan 19 '21 at 11:42
  • no it didn't @daniu My map doesn't have a map as a param. – real_G Jan 19 '21 at 12:33
  • How do you mean? You can take the `Pair` from the accepted answer and use it as the key of your map. You may want to post example code how you intend to actually use it. – daniu Jan 19 '21 at 12:54

0 Answers0