mengdiwang / guava-libraries

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

Please explain why Lists doesn't have filter method #505

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I can see in idea graveyard that Lists.filter is one of rejected ideas. Could 
you please explain there why?

My motivation for Lists.filter is the need for transforming Iterable to List 
when using existing code requiring List as input. It would be more readable to 
use Lists.filter(*) instead of Lists.newArrayList(Collections2.filter(*))...

Original issue reported on code.google.com by kopperni...@gmail.com on 27 Dec 2010 at 7:51

GoogleCodeExporter commented 9 years ago
Hey koppernickus,

This was already discussed in the google-collections issues / group, but your 
question is legitimate since there is no mention of it in the guava project's 
issues / group ;)

Guava does not provide such a method, because the filtered List view would have 
very poor performance.
They could have Lists.filter() return a filtered *copy* instead, but:
- it would be counter-intuitive, since the similarly named 
Collections2.filter() and Iterables.filter() both returns *views* instead of 
copies
- there is the problem of the choice of the List implementation to return: 
should we return an ArrayList? an ImmutableList? a LinkedList?

For these reasons, it is preferable to let the developer do the copy himself:
ImmutableList.copyOf(Iterables.filter(*))
Lists.newArrayList(Iterables.filter(*))
...

If you really want the syntactical sugar, and you know which implementation 
type you want to use in your project, you could create a static utility method 
that hides this boilerplate.

More info:
http://code.google.com/p/google-collections/issues/detail?id=7#c2
http://code.google.com/p/google-collections/issues/detail?id=98#c1
http://groups.google.com/group/google-collections-users/browse_thread/thread/2f0
120513488977e
http://groups.google.com/group/google-collections-users/browse_thread/thread/5ad
e208a52ee3973
http://groups.google.com/group/google-collections-users/msg/fac903c3a542d77f

Original comment by nev...@gmail.com on 30 Dec 2010 at 11:45

GoogleCodeExporter commented 9 years ago
Woops, just saw the related thread in the Google Group. It seems like you 
already knew all of this...

Anyway, I'll link it here, since it's related:
http://groups.google.com/group/guava-discuss/browse_thread/thread/278dd6a99214d9
1a

Original comment by nev...@gmail.com on 31 Dec 2010 at 12:15

GoogleCodeExporter commented 9 years ago
Seems like the question has been sufficiently answered.

Original comment by boppenh...@google.com on 31 Dec 2010 at 4:54

GoogleCodeExporter commented 9 years ago
Thanks for explanation, but what I really meant was explaining it on Wiki.
Most of the users don't read forum, they just go to Wiki pages. If you put
link to this thread in this page
http://code.google.com/p/guava-libraries/wiki/IdeaGraveyard I would be happy
:-)

Original comment by kopperni...@gmail.com on 31 Dec 2010 at 9:57

GoogleCodeExporter commented 9 years ago
Great idea, its done.

Original comment by boppenh...@google.com on 31 Dec 2010 at 6:47

GoogleCodeExporter commented 9 years ago
Issue 848 has been merged into this issue.

Original comment by wasserman.louis on 28 Dec 2011 at 5:55

GoogleCodeExporter commented 9 years ago
Issue 958 has been merged into this issue.

Original comment by wasserman.louis on 4 Apr 2012 at 2:04

GoogleCodeExporter commented 9 years ago
Issue 959 has been merged into this issue.

Original comment by cgdec...@gmail.com on 4 Apr 2012 at 3:20

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