Closed GoogleCodeExporter closed 9 years ago
getFirst(Iterator<T>, T defaultValue) is the only version of this that's worth
having, as without a default, you're better off just calling
theIterable.iterator().next().
Original comment by kevin...@gmail.com
on 11 Aug 2009 at 1:26
>as without a default, you're better off just calling
>theIterable.iterator().next().
Calling iterable.iterator().next() is simply not readable - sorry. It is
failing in
the same way as most of the JDK collection libraries fail (too low level,
missing
readability, ...).
Original comment by sebastian.jancke@googlemail.com
on 12 Aug 2009 at 7:20
Another issue about this: consistency. There is getLast(iterable/iterator).
Either
you break the API to getLast(.., default) or you have to be consistent with
getFirst.
Original comment by sebastian.jancke@googlemail.com
on 12 Aug 2009 at 7:24
I couldn't disagree more on either count.
iterable.iterator().next() is perfectly clear and readable and _unambiguous_.
I know
exactly what it does, whereas with Iterators.getFirst(), I have to run off and
look
up how that library designer decided to do it.
Also, your notion of consistency is deeply misguided. We use consistency in
how we
present important functionality, but we never use it to justify adding
worthless
functionality, and you shouldn't in your own libraries either!
Original comment by kevin...@gmail.com
on 12 Aug 2009 at 5:01
Original comment by kevin...@gmail.com
on 12 Aug 2009 at 5:55
To me and collegues, "iterable.iterator().next()" is not readable. It says:
"give me
an iterator and then the next element, that is the first". That does not comply
with
the sense of readability stated by RC Martin and others. It is not ambigous,
you are
right. But "getFirst" states clearly what it does - retrieving the first
element.
I agree on not adding worthless functionality. Maybe highest readability is not
a
design goal of this library (I do not judge such a decision...)?
Still, having getFirst ist consistent with having getLast. It was the first
thing I
noticed and missed, when using getLast on a collection. Maybe it's because I'm
used
to it from .NET/LINQ code. There you have a whole bunch of clearly readabily
high-level functions on collections - getFirst is one of them and lot's of
others are
already part of this library.
In the end - It was just a suggestion and expression of my experience with this
very
nice library. I won't comment more on this, maybe you implement it or you
decide not
to - both is ok (as I can still add getFirst through AJ).
Original comment by sebastian.jancke@googlemail.com
on 14 Aug 2009 at 8:37
.iterator().next() is a extremely well-established java idiom, since it is the
only way
to get the first element of a Collection or a Set for at least a _decade_ now.
So
"readability" concerns are really moot, except for perhaps very
recent/inexperienced
java developers (who are clearly *not* the target audience of this library).
Coming by
2009 and introducing a longer version of something as common and which works
perfectly
would be a very strange decision for google collections.
Original comment by jim.andreou
on 14 Aug 2009 at 10:08
Original comment by kevin...@gmail.com
on 17 Sep 2009 at 5:01
Original comment by kevin...@gmail.com
on 17 Sep 2009 at 5:45
Original comment by kevin...@gmail.com
on 17 Sep 2009 at 5:57
I don't think I ever made it clear: I believe we *should* add an overload
accepting a
"T defaultValue" to each Itera*s method we currently have that accepts an
Iterable<T>,
returns a T, and throws an exception if no suitable T is found. However many
methods
that is.
Original comment by kevinb@google.com
on 16 Mar 2010 at 8:04
Original comment by kevinb@google.com
on 30 Jul 2010 at 3:50
Looks like we've done this get, getLast and getOnlyElement but just need to do
it for find and we're done.
Original comment by kevinb@google.com
on 30 Aug 2010 at 5:33
Wait, no, we never did the *first* part of this which is to add getFirst(Iter,
T).
And forget what I said about find(), because that's tracked in bug 150. That's
a little weird but that's how it is so let's just keep it that way.
Original comment by kevinb@google.com
on 30 Aug 2010 at 5:39
Original comment by boppenh...@google.com
on 30 Aug 2010 at 5:59
Committed in r105.
Original comment by boppenh...@google.com
on 4 Sep 2010 at 5:38
Please reconsider adding Itera*s.getFirst/getNext(Itera* i)
For Iterables, the following methods exist:
* public static <T> T get(Iterable<T> i, int p)
* public static <T> T get(Iterable<T> i, int p, T defVal)
* public static <T> T getFirst(Iterable<T> i, T defVal)
* public static <T> T getLast(Iterable<T> i)
* public static <T> T getLast(Iterable<T> i, T defVal)
* public static <T> T getOnlyElement(Iterable<T> i)
* public static <T> T getOnlyElement(Iterable<T> i, T defVal)
It really looks like something is missing.
My colleagues who just start Java keep bugging me about the fact that
iterator().next() is just not understandable at first sight.
Original comment by ogregoire
on 23 Nov 2010 at 2:38
Can we also add a checked version Iterables#getFirst that takes no default
value and throws a NoSuchElementException?
It would work exactly like Iterables.getLast(T).
I often write Iterables.getFirst(it, null) and then check for null.
Original comment by ori.schw...@gmail.com
on 22 Dec 2011 at 8:43
[deleted comment]
It's already mentioned that you need to call - iterable.iterator().next()(which
will throw NoSuchElementException) than Iterables.getFirst(it, null) and then
check for null.
Original comment by ipremra...@gmail.com
on 24 Dec 2011 at 10:01
How about returning an Optional on getFirst?
public static <T> Optional<T> getFirst(Iterable<T> i)
Original comment by mar...@aie.pl
on 16 May 2013 at 8:09
We did that for FluentIterable. I doubt it's worth going back and fixing
Iterables now. Use FluentIterable!
Original comment by kevinb@google.com
on 22 May 2013 at 4:22
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:16
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:10
Original issue reported on code.google.com by
sebastian.jancke@googlemail.com
on 11 Aug 2009 at 8:53