I frequently have this problem but didn't find it as an established programming pattern. I have some Class C whose equality is determined by some key k and I want to prevent time-consuming initialisation of additional equal objects.This is my implementation but the synchronized get method prevents parallel instantiation.Class C{ Key key; private static Map<Key,C> cs = new HashMap<Key,C>(); private C(Key key) {  // this takes a long time } synchronized static C get(Key key) {  C c = cs.get(key);  if(c==null)  {   c = new C(key);   cs.put(key,c);  }  return c; } @Override public boolean equals(Object o) {    if (this == o) return true;    if (o == null) return false;    if (getClass() != o.getClass()) return false;    C other = (C) o;    if (key == null)    {        if (other.key != null) return false;    }    else if (!key.equals(other.key)) return false;    return true; } @Override public int hashCode() {..}}Can you point me to an existing best-practise to this, if it exists, or show me how to implement this optimally? My criteria are clarity, brevity, performance and thread-safeness.
How to ensure objects unique by equality?
java;design patterns