mengdiwang / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Collections2.FilteredCollection should implement equals and hashCode #509

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

    @Test
    public void testEqualityForFilteredCollection() throws Exception {
        Collection<String> s = ImmutableList.of("a");
        assertEquals(filter(s, alwaysTrue()), s);
    }

What is the expected output? What do you see instead?

I expect pass but it fails.

What version of the product are you using? On what operating system?

r07

Please provide any additional information below.

Collections2.FilteredCollection doesn't implement equals and hashCode.

Original issue reported on code.google.com by kopperni...@gmail.com on 29 Dec 2010 at 3:39

GoogleCodeExporter commented 9 years ago
There was some discussion about this internally, but we decided against adding 
this.  Here is some of the points we talked about:

 - Would we use order-dependent or order-independent equality?  The appropriate answer would depend on what type of Collection is being used with filter().
 - For some collections, (for instane some Queues) [a,b] is not necessarily "equal" to [a,b].
 - Similar JDK and guava wrappers such as synchronizedCollection, checkedCollection, and unmodifiableCollection don't implement equals.

For your specific case, I would recommend 
assertTrue(Iterables.elementsEqual(filter(s, alwaysTrue()), s)).

Original comment by boppenh...@google.com on 30 Dec 2010 at 1:21

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Thanks for explaining. This is clearly my mistake. I should use Lists.filter 
but since there's no such method I (mistakenly) used the one from Collections2.

I think I should use
ImmutableList.copyOf(Iterables.filter(s))
to get List as filtering result.

Original comment by kopperni...@gmail.com on 30 Dec 2010 at 6:50

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09