On 01/08/18 20:46, Peter Levart wrote:

Or better yet, compute the size of the other set as you iterate the elements. Like this:

        public boolean equals(Object o) {
            if (o == this)
                return true;

            if (!(o instanceof Set))
                return false;

            int osize = 0;
            for (Object e : (Iterable<?>) o) {
                if (!contains(e)) {
                    return false;
                }
                osize++;
            }
            return size() == osize;
        }

...since calling .size() on the passed-in Set might not be free.


Ops, forgot to check for null 'e':

        @Override
        public boolean equals(Object o) {
            if (o == this)
                return true;

            if (!(o instanceof Set))
                return false;

            int osize = 0;
            for (Object e : (Iterable<?>) o) {
                if (e == null || !contains(e)) {
                    return false;
                }
                osize++;
            }
            return size() == osize;
        }


Regards, Peter

Reply via email to