Iterables.find throws NoSuchElementException,why not return null ?

382 views
Skip to first unread message

fachhoch

unread,
Apr 19, 2011, 12:55:55 PM4/19/11
to guava-discuss
I am new bee, Iterables.find throws NoSuchElementException , I am
using this method to find if the collection has an item which matches
the predicate, why should it throw NoSuchElementException? why not
just return null ? , now I have to catch this exception everytime I
want to find something in collection , or is there any other method
to search the collection for any given predicate which I can use
without any try catch block?

Kurt Alfred Kluever

unread,
Apr 19, 2011, 12:57:51 PM4/19/11
to fachhoch, guava-discuss
Try the 3-arg version:

Iterables.find(iterable, predicate, /* default value */ null);




--
Kurt Alfred Kluever | k...@google.com | (617) 500-KURT

David P. Baker

unread,
Apr 19, 2011, 1:04:10 PM4/19/11
to Kurt Alfred Kluever, fachhoch, guava-discuss
Or, try Iterables.any(Iterable, Predicate). It returns true if at least one element matches the predicate.

--dpb

fachhoch

unread,
Apr 19, 2011, 1:11:57 PM4/19/11
to guava-discuss
I am using 1.0 and there is no method with three arguments which
version has this ?
> > To get help:http://stackoverflow.com/questions/ask(use the tag "guava")

Kurt Alfred Kluever

unread,
Apr 19, 2011, 1:14:38 PM4/19/11
to fachhoch, guava-discuss
We're up to Guava release 9 now!  The 3 arg version of Iterables.find was added in release 7.

You might want to grab the latest release (we've made a lot of improvements since then): http://code.google.com/p/guava-libraries/downloads/list

To get help: http://stackoverflow.com/questions/ask (use the tag "guava")

Kevin Bourrillion

unread,
Apr 19, 2011, 1:26:01 PM4/19/11
to Kurt Alfred Kluever, fachhoch, guava-discuss
I expect he is referring to the old "Google Collections Library." <heavy sigh>

It is really sad to me how many people keep using that.  There's absolutely zero good reason to use it instead of Guava (and it's dangerous, as you won't notice a problem if both that and Guava wind up in an app's classpath at the same time EEEK) and I just don't know how to get that idea to spread. :-(

Kevin Bourrillion @ Google
http://guava-libraries.googlecode.com

James Moore

unread,
Apr 19, 2011, 1:54:56 PM4/19/11
to Kevin Bourrillion, Kurt Alfred Kluever, fachhoch, guava-discuss
On Tue, Apr 19, 2011 at 10:26 AM, Kevin Bourrillion <kev...@google.com> wrote:
>
> I expect he is referring to the old "Google Collections Library." <heavy sigh>
> It is really sad to me how many people keep using that.  There's absolutely zero good reason to use it instead of Guava (and it's dangerous, as you won't notice a problem if both that and Guava wind up in an app's classpath at the same time EEEK) and I just don't know how to get that idea to spread. :-(

You might change some of the text on the google collections website
(http://code.google.com/p/google-collections/).  My suggestions:

This project has evolved into Guava!

Replace with: Google collections has been replaced by Guava, a new
project from Google.
Why: 1.  "evolved into" isn't strong enough.  2.  Make it clear that
it's still a Google project.  This is a big deal; I suspect a lot of
people think they'd rather stick with "Google Collections" since it
clearly comes from Google.   Given a choice between the thing from
Google, and the thing with a funny name from somewhere else, why would
I even look at the fruit-like project?

"We have created a project called Guava to house the Google Collections"

This tells me Google Collections still lives!  Yes!  I'll just use it
instead of guava, since I don't really care about the other guava
stuff.  And I'll stop reading now, since I'm lazy.

I'd say something like:

Google collections has been replaced by Guava, a new project from Google.

Guava is a fully compatible, proper superset of the old, deprecated,
Google Collections Library. Guava includes six months worth of bug
fixes, doc fixes, and performance fixes. [Say something here about
how the core contributors come from Google; you probably have a
standard way you're supposed to say that. "The core Guava team is
composed of engineers from Google, with contributions from blah blah
blah?"]

...

Google Collections is no longer maintained and has been completely
replaced by Guava. We apologize for any confusion this causes.

--
James Moore
ja...@restphone.com
http://jamesmoorecode.blogspot.com/

Kevin Bourrillion

unread,
Apr 19, 2011, 2:05:57 PM4/19/11
to jamest...@gmail.com, Kurt Alfred Kluever, fachhoch, guava-discuss
Thanks for the suggestions, and I'll use them if I find time to make edits, but I can't help thinking that even the perfect wording of this page would have only stopped a few percent of new users anyway.  That is I expect that most of them aren't really seeing this page anyway, somehow.


Blair Zajac

unread,
Apr 19, 2011, 3:44:45 PM4/19/11
to Kevin Bourrillion, guava-discuss
On Apr 19, 2011, at 11:05 AM, Kevin Bourrillion wrote:

> Thanks for the suggestions, and I'll use them if I find time to make edits, but I can't help thinking that even the perfect wording of this page would have only stopped a few percent of new users anyway. That is I expect that most of them aren't really seeing this page anyway, somehow.

Cut a new jar that only throws UnsupportedOperationException("Use Guava instead.") :)

Blair

Kevin Bourrillion

unread,
Apr 19, 2011, 3:58:30 PM4/19/11
to Blair Zajac, guava-discuss
Oh, that's goood.  google-collect-2.0!

Louis Wasserman

unread,
Apr 19, 2011, 4:02:07 PM4/19/11
to Kevin Bourrillion, Blair Zajac, guava-discuss
That is a joke, right?

Kevin Bourrillion

unread,
Apr 19, 2011, 4:07:17 PM4/19/11
to Louis Wasserman, Blair Zajac, guava-discuss
  public boolean clarifyWhetherThatWasAJokeOrNot() {
    throw new UnsupportedOperationException();

Colin Decker

unread,
Apr 22, 2011, 5:52:02 PM4/22/11
to guava-discuss
One thing that might help a tiny bit would be if we could get one more vote for [google-collections] as a synonym for [guava] on stack overflow (http://stackoverflow.com/tags/guava/synonyms). Then I believe any question tagged with [google-collections] would automatically have that tag renamed to [guava].

-- 
Colin

Maaartin G

unread,
Apr 22, 2011, 8:41:39 PM4/22/11
to guava-...@googlegroups.com
It seems to have already been aliased - at least I can find no place to upvote it (I'm allowed to do it -- since yesterday :D). I'm not sure, how aliasing exactly works, but I think it doesn't do the right think. For example, [javase] and [java-api] are aliased to [java], but there are some fairly old questions still tagged with them. No renaming for [google-collections] have happened yet, but it may take some time as it's probably done in a batch job. Let's see tomorrow.

Colin Decker

unread,
Apr 22, 2011, 8:48:25 PM4/22/11
to guava-...@googlegroups.com
Yeah, I think someone put in the last vote pretty quickly after I said that. I don't think it'll go back through and replace all google-collections tags with guava, but future questions tagged google-collections should be automatically retagged as guava. I think most questions tagged google-collections already have the guava tag currently... I could perhaps go through and add it to any that don't.

-- 
Colin


On Fri, Apr 22, 2011 at 8:41 PM, Maaartin G <graj...@seznam.cz> wrote:
It seems to have already been aliased - at least I can find no place to upvote it (I'm allowed to do it -- since yesterday :D). I'm not sure, how aliasing exactly works, but I think it doesn't do the right think. For example, [javase] and [java-api] are aliased to [java], but there are some fairly old questions still tagged with them. No renaming for [google-collections] have happened yet, but it may take some time as it's probably done in a batch job. Let's see tomorrow.

--

Maaartin G

unread,
Apr 22, 2011, 10:30:11 PM4/22/11
to guava-...@googlegroups.com
On Saturday, April 23, 2011 2:48:25 AM UTC+2, Colin Decker wrote:
Yeah, I think someone put in the last vote pretty quickly after I said that. I don't think it'll go back through and replace all google-collections tags with guava, but future questions tagged google-collections should be automatically retagged as guava. I think most questions tagged google-collections already have the guava tag currently... I could perhaps go through and add it to any that don't.

I think it can be done automatically somehow, try http://meta.stackoverflow.com/tags/retag-request/info


 

Colin Decker

unread,
Apr 22, 2011, 11:47:36 PM4/22/11
to guava-...@googlegroups.com
I'm not sure we want to have all the existing google-collections tags replaced by guava tags, though it'd probably be nice to have everything that's tagged google-collections have a guava tag as well. Some of the questions that don't have the guava tag already have the maximum number of tags though. Anyone else have any thoughts on eliminating the google-collections tag entirely?

Interestingly, if you search for [google-collections] with the search box, it searches for [guava] instead. If you click on an existing google-collections tag link, you get the google-collections tagged stuff.

-- 
Colin




 

--

Maarten Billemont

unread,
Apr 24, 2011, 2:36:20 PM4/24/11
to fachhoch, guava-discuss
This is something that also seemed odd to me before.

In our development team, we've always had the habit to use 'find' prefixes for action methods that can yield null and 'get' prefixes for action methods that are guaranteed to throw an exception rather than allow a null return value.

Eg.
handler = findProtocolHandler( httpRequest );
if (handler == null)
redirectToMainPage( httpResponse );
else
handler.handle( httpRequest, ... );

Vs.
getProtocolHandler( httpRequest ).handle( httpRequest, ... );

I just invented this code on the spot so it may not be the best example, but it illustrates how 'find' indicates that a null check needs to be done while 'get' would indicate that such a check is not required. Obviously, this only works for action methods ("do something with this object and your state to calculate the result value") and not type accessors ("user.getAddress()").

I was just wondering whether this is a common habit, and if so, whether in that light this method might benefit from a rename.

Reply all
Reply to author
Forward
0 new messages