I know that it is necessary to do so that the mutable object does not return and the encapsulation is not lost:
public class CubeWarehouse{ private Set<Cube> cubeSet = new HashSet<>; public Set<Cube> getWareHouse(){ return Collections.unmodifiableSet(cubeSet); } } Do we have to do this if we return an array:
public Cube[] getArray(){ return Collections.unmodifiableSet(cubeSet).toArray(new Cube[0]); }