MyData (Integer var) { if (myInitialized.contains(var)) { Set<Integer> newOverInitialized; if (myOverInitialized.contains(var)) { newOverInitialized = myOverInitialized; } else { newOverInitialized = new HashSet<>(myOverInitialized); newOverInitialized.add(var); } return new MyData(myInitialized, newOverInitialized); } else { HashSet<Integer> newInitialized = new HashSet<>(myInitialized); newInitialized.add(var); return new MyData(newInitialized, myOverInitialized); } }