Search code examples
javacollectionsforeachencapsulation

How to return collections' data without returning a collection itself?


I have a class (A.java) that contains two private fields of type ArrayList and HashMap.
I also have another class (B.java) that should have access to their data. I could make two getters, but I don't want to return my collections as is. Class B.java should only have access to data, not to add(), isEmpty(), containsKey() etc.
Can I return my collections in such way, so I could somehow use it with foreach in class B somehow but without giving the possibility to modify them?


Solution

  • Don't return a collection, return a Stream. That way it is easy for the user to know that they are getting a stream of objects, not a collection. And it's easy to change the implementation of the collection without changing the way it's used. It's trivial for the user to filter, map, reduce collect etc.

    So:

    class A {
        private List<C> cs = new ArrayList<>();
    
        public Stream<C> getCs() {
            return cs.stream();
        }
    }
    
    class B {
        public void processCs(A a) {
            a.getCs().filter(C::hasFooness).forEach(...);
        }
    }