int (EqClass eqClass) { int freeIndex = myEqClasses.indexOf(null); int resultIndex = freeIndex >= 0 ? freeIndex : myEqClasses.size(); if (freeIndex >= 0) { myEqClasses.set(freeIndex, eqClass); } else { myEqClasses.add(eqClass); } eqClass.forValues(id -> myIdToEqClassesIndices.put(id, resultIndex)); return resultIndex; }