Iterables.find() throws java.util.NoSuchElementException if no no matches

1,163 views
Skip to first unread message

Pangea

unread,
Oct 30, 2009, 1:50:44 PM10/30/09
to Google Collections Library - users list
I am getting the below exception when the data i am looking for is not
found in the List...appreciate if anyonce can let me know if i am
doing something wrong


boolean isRequested(Boolean flag, final String... dataTypeIdentifiers)
{
final ArrayList<String> checkOn = Lists.newArrayList
(dataTypeIdentifiers);

if (flag == null)
{
DataType dataType = Iterables.find(requestedDataTypes, new
Predicate<DataType>() {
public boolean apply(DataType dt)
{
return checkOn.contains(dt.getIdentifier());
}
});

flag = (dataType != null);
}
return flag;
}

java.util.NoSuchElementException
at com.google.common.collect.AbstractIterator.next
(AbstractIterator.java:145)
at com.google.common.collect.Iterators.find(Iterators.java:669)
at com.google.common.collect.Iterables.find(Iterables.java:558)
at com.consumersearch.impl.AlertsImpl.isRequested(AlertsImpl.java:718)

Kevin Bourrillion

unread,
Oct 30, 2009, 2:04:06 PM10/30/09
to Pangea, Google Collections Library - users list
How about this?

  boolean isRequested(String... dataTypeIdentifiers) {
    Set<String> checkOn = ImmutableSet.of(dataTypeIdentifiers);
    for (DataType dt : requestedDataTypes) {
      if (checkOn.contains(dt.getIdentifier())) {
        return true;
--
Kevin Bourrillion @ Google
internal:  http://go/javalibraries
external: guava-libraries.googlecode.com

Pangea

unread,
Oct 30, 2009, 2:11:44 PM10/30/09
to Google Collections Library - users list
OK...the api is coded to throw NoSuchElementException in this case,
and it is documented in the code. However the doc for throws clause is
not showing up in ur generated javadoc...

I fixed this issue by using any() method as all i need is to find if
the given data exists in the collection or not. can i assume that any
() method stops at the first match instead of iterating over the
entire collection?

Kevin Bourrillion

unread,
Oct 30, 2009, 2:19:37 PM10/30/09
to Pangea, Google Collections Library - users list
On Fri, Oct 30, 2009 at 11:11 AM, Pangea <yara...@gmail.com> wrote:

OK...the api is coded to throw NoSuchElementException in this case,
and it is documented in the code. However the doc for throws clause is
not showing up in ur generated javadoc...

 

I fixed this issue by using any() method as all i need is to find if
the given data exists in the collection or not. can i assume that any
() method stops at the first match instead of iterating over the
entire collection?

Yep, it does.

-- 

Pangea

unread,
Oct 30, 2009, 2:27:50 PM10/30/09
to Google Collections Library - users list
regarding javadc...my bad i was looking at the javadoc of fileter()
method...

On Oct 30, 2:19 pm, Kevin Bourrillion <kev...@google.com> wrote:
> On Fri, Oct 30, 2009 at 11:11 AM, Pangea <yarav...@gmail.com> wrote:
>
> > OK...the api is coded to throw NoSuchElementException in this case,
> > and it is documented in the code. However the doc for throws clause is
> > not showing up in ur generated javadoc...
>
> I see it here:http://google-collections.googlecode.com/svn/trunk/javadoc/com/google...)

Pangea

unread,
Oct 30, 2009, 2:39:52 PM10/30/09
to Google Collections Library - users list
yep this was my first option but this predicate will be reused at many
places and hence the deicision to jave a new class
Reply all
Reply to author
Forward
0 new messages