Filter Lists

14 views
Skip to first unread message

Benjamin Darfler

unread,
Nov 4, 2009, 4:23:01 PM11/4/09
to Google Collections Library - users list
It looks like there is no List filter(List, Predicate) method. Am I
missing something?

Kevin Bourrillion

unread,
Nov 4, 2009, 4:52:39 PM11/4/09
to Benjamin Darfler, Google Collections Library - users list
Sounds like you're aware that you can get the result as an Iterable or as a Collection using the corresponding methods in the Iterables and Collections2 classes.  The reasons to specifically want a List to be returned would be the indexed-based methods like get(n).  However, the performance characteristics of these methods on a filtered list would be... sad.  We think that if you really want a list, you should use ImmutableList.copyOf(Iterables.filter()).

This is going into the FAQ that I swear I'm working on!



On Wed, Nov 4, 2009 at 1:23 PM, Benjamin Darfler <b...@locamoda.com> wrote:

It looks like there is no List filter(List, Predicate) method. Am I
missing something?




--
Kevin Bourrillion @ Google
internal:  http://go/javalibraries
external: guava-libraries.googlecode.com

Ben Darfler

unread,
Nov 4, 2009, 5:02:01 PM11/4/09
to Kevin Bourrillion, Google Collections Library - users list
Thanks!
--
Software Engineer
LocaModa
Connecting People & Places

Johan Van den Neste

unread,
Nov 5, 2009, 4:35:39 AM11/5/09
to Ben Darfler, Kevin Bourrillion, Google Collections Library - users list
Don't want to start a new thread for this one even though it is not
very related:

I'm missing find/filter Iterable methods that return indices rather
than the objects themselves. For lists, this is more natural.
Implementing an index-based filter on top of the existing filter is
very clumsy compared to doing it the other way around. (again, only
for lists, that is)

But I understand that this could be regarded as convenience and would
conflict with 'keeping things minimal'.
--
Johan Van den Neste

Kevin Bourrillion

unread,
Nov 5, 2009, 10:20:29 AM11/5/09
to Johan Van den Neste, Ben Darfler, Google Collections Library - users list
Please file an enhancement request for a Lists.find() method returning
the index.

Jared Levy

unread,
Nov 5, 2009, 12:45:39 PM11/5/09
to Kevin Bourrillion, Johan Van den Neste, Ben Darfler, Google Collections Library - users list
How about creating Lists.indexOf(List, Predicate) and Lists.lastIndexOf(List, Predicate) methods?

Kevin Bourrillion

unread,
Nov 5, 2009, 12:52:14 PM11/5/09
to Jared Levy, Johan Van den Neste, Ben Darfler, Google Collections Library - users list
That's what I meant, just didn't want to spend though on what's the best name yet.  Maybe indexOfFirstMatch(), or maybe indexOf() is fine.  Not sure the lastIndexOf() version is needed as much.

Rogan Creswick

unread,
Nov 5, 2009, 2:19:09 PM11/5/09
to Kevin Bourrillion, Jared Levy, Johan Van den Neste, Ben Darfler, Google Collections Library - users list
On Thu, Nov 5, 2009 at 9:52 AM, Kevin Bourrillion <kev...@google.com> wrote:
> That's what I meant, just didn't want to spend though on what's the best
> name yet.  Maybe indexOfFirstMatch(), or maybe indexOf() is fine.  Not sure
> the lastIndexOf() version is needed as much.

Just tossing this out because it came to mind:

Iterator<Integer> Lists.indexes(List, Predicate)

The other variations can be implemented on top of that, it
encapsulates the common (in my experience) loop over all indices, and
it can be implemented lazily.

I don't think I feel strongly about the name ;) but returning an
Iterator would be more useful to me than returning atomic values.

--Rogan

Kevin Bourrillion

unread,
Nov 5, 2009, 2:50:12 PM11/5/09
to Rogan Creswick, Jared Levy, Johan Van den Neste, Ben Darfler, Google Collections Library - users list
It makes sense, but I have some difficulty imagining it really getting used.  Feel free -- whoever does actually file this request :-) -- to include this option.

It might as well be Iterable, not Integer.  You still wouldn't want to implement lastIndexOf() atop that, tho.

fishtoprecords

unread,
Nov 5, 2009, 4:33:21 PM11/5/09
to Google Collections Library - users list
> I'm missing find/filter Iterable methods that return indices rather
> than the objects themselves. For lists, this is more natural.
> Implementing an index-based filter on top of the existing filter is
> very clumsy compared to doing it the other way around. (again, only
> for lists, that is)

A related approach would be a transforming function that applies the
filter on the object and returns a List of indecies. But you would
expect a sublist, where the Predicate matches, rather than a complete
list.

Should be fairly easy to implement in specific, but I'm not sure how
to extend it in general
Reply all
Reply to author
Forward
0 new messages