Java backend: fixed a wrong fix by me; added additional check to prevent the objects from being changed after they are added into a HashSet