Using Iterables.filter() to make a non-Immutable list?

167 views
Skip to first unread message

fishtoprecords

unread,
Dec 19, 2009, 6:59:20 PM12/19/09
to Google Collections Library - users list
I have fallen in love with Functions and Predicates. They are great.

But sometimes, you really want a boring old ArrayList instead of an
ImmutableList.

I'm not finding a clean and short way to do this.

I've got code such as:

Iterable<Calendar> itHour = Iterables.filter(recentProcessed,
new Predicate<Calendar> () {
public boolean apply(Calendar cal) {
long delta = Calendar.getInstance
(Constants.utcTZ).getTimeInMillis() - cal.getTimeInMillis();
return delta/Constants.SECONDS_PER_HOUR == 0;
}
});
processedHour = new ArrayList<Calendar>();
for (Calendar c : itHour) {
processedHour.add(c);
}

Anybody got a better solution, something that will accept the
Iterable<> and turn it into a writable list/map/set.

Thanks
Pat

Nikolas Everett

unread,
Dec 19, 2009, 7:06:41 PM12/19/09
to fishtoprecords, Google Collections Library - users list
Lists.newArrayList(oldList) should do it.  Also, why not use TimeUnit.HOURS.toSeconds(1)  instead of Constants.SECONDS_PER_HOUR?


--
Google Collections Library - users list
http://groups.google.com/group/google-collections-dev?hl=en

To unsubscribe, send email to:
google-collections...@googlegroups.com

fishtoprecords

unread,
Dec 19, 2009, 7:43:13 PM12/19/09
to Google Collections Library - users list
On Dec 19, 7:06 pm, Nikolas Everett <nik9...@gmail.com> wrote:
> Lists.newArrayList(oldList) should do it.  Also, why not use
> TimeUnit.HOURS.toSeconds(1)  instead of Constants.SECONDS_PER_HOUR?


Thanks, that is just what I was looking for.

I didn't know about the TimeUnit.HOURS.toSeconds(1), using it means I
get to kill some of my local magic constants. Thanks again

Dimitris Andreou

unread,
Dec 19, 2009, 8:23:26 PM12/19/09
to fishtoprecords, Google Collections Library - users list
2009/12/20 fishtoprecords <pat2...@gmail.com>:

> I have fallen in love with Functions and Predicates. They are great.
>

I have the strange suspicion that Kevin is face-palming maniacly when
he hears that one :)

fishtoprecords

unread,
Dec 19, 2009, 9:25:19 PM12/19/09
to Google Collections Library - users list
> I have the strange suspicion that Kevin is face-palming maniacly when
> he hears that one :)

So is there an inside story here you can share? Did he fight for them
against big odds? Did he argue that no one would understand them, let
alone use them?

I've almost got my PointyHairedBoss using them. Mostly because I'm
using them everywhere, they fit wonderfully into the app that I'm
writing.

Reply all
Reply to author
Forward
0 new messages