You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Matthias Pfau <mp...@northernideas.de> on 2006/01/24 22:48:18 UTC

[lang] EqualsBuilder: Comparison of Collections

Hi,
this is my first post ever to a mailing list so please do not mind to tell
me if I do sth. wrong... ;)

I just asked myself why the EqualsBuilder does not support the comparison of
Collections. Personally, I am very often in the situation that I have to
compare Collections. Collections are widely used and there might be other
people out there that need a proper support for this case. As you might know
the problem when just calling .equals(other) on an object is, that both
Collections need to have the same order. But two Collections should
definitely be equal if they contain the same elements, ordering is not
important!

The following code respects this and is my proposal to enhance the
EqualsBuilder.

--snip-->

    public EqualsBuilder append(Collection lhs, Collection rhs) {
        if (isEquals == false) {
            return this;
        }
        if (lhs == rhs) {
            return this;
        }
        if (lhs == null || rhs == null) {
            this.setEquals(false);
            return this;
        }
        if (lhs.size() != rhs.size()) {
            this.setEquals(false);
            return this;
        }
        if (lhs.size() == Integer.MAX_VALUE) {
            if (!lhs.containsAll(rhs) || !rhs.containsAll(lhs)) {
                this.setEquals(false);
            }
            return this;
        }
        if (!lhs.containsAll(rhs)) {
            this.setEquals(false);
        }
        return this;
    }

<--snap--

Looking forward to your comments!

Kind Regards,
Matthias Pfau


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


RE: [lang] EqualsBuilder: Comparison of Collections

Posted by Matthias Pfau <mp...@northernideas.de>.
Hello,

> Stephen Colebourne:
> This is a misunderstanding of the java collections framework.
> 
> The framework consists of three collections interfaces - 
> Collection, Set and List.
> 
> Two List implementations are equal based on their order.
> Two Set implementations are equal based on their content.
> Two Collection implementations have no defined equals behaviour.
> 
> If you want to achieve equals comparison based on content 
> without order, then simply create two HashSet objects instead 
> of two ArrayList objects.

Thank you for welcoming and correcting me on this Stephen, you are
absolutely right! You proposed to use a HashSet in order to compare
Collections for equality based on their content. However, in the current
State of the EqualsBuilder, it is not possible to use it in order to compare
HashSets... 

So wouldn't it be useful to add such an operation?

Kind Regards,
Matthias


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [lang] EqualsBuilder: Comparison of Collections

Posted by Stephen Colebourne <sc...@btopenworld.com>.
Matthias Pfau wrote:
> this is my first post ever to a mailing list
Welcome

> I just asked myself why the EqualsBuilder does not support the comparison of
> Collections. Personally, I am very often in the situation that I have to
> compare Collections. Collections are widely used and there might be other
> people out there that need a proper support for this case. As you might know
> the problem when just calling .equals(other) on an object is, that both
> Collections need to have the same order. But two Collections should
> definitely be equal if they contain the same elements, ordering is not
> important!

This is a misunderstanding of the java collections framework.

The framework consists of three collections interfaces - Collection, Set 
and List.

Two List implementations are equal based on their order.
Two Set implementations are equal based on their content.
Two Collection implementations have no defined equals behaviour.

If you want to achieve equals comparison based on content without order, 
then simply create two HashSet objects instead of two ArrayList objects.

Stephen

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org