public boolean hasSolutionWith(MyBooleanExpression other) { if (hasSolutionWithRes == null) { hasSolutionWithRes = new HashMap<>(); } if (hasSolutionWithRes.containsKey(other)) { return hasSolutionWithRes.get(other); } // possible to rewrite in a smarter way MyBooleanExpression e = null; try { e = new MyBooleanExpression("(" + repr + ")&(" + other.repr + ")"); } catch (ParseException ex) { ex.printStackTrace(); System.exit(1); } boolean res = e.hasSolution(); hasSolutionWithRes.put(other, res); return res; }