Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Itera*s.getFirst(Iterator<T>, T) - and add the ", T" overloads for get/getLast/getOnlyElement #217

Closed
gissuebot opened this issue Oct 31, 2014 · 21 comments
Labels
status=fixed type=enhancement Make an existing feature better
Milestone

Comments

@gissuebot
Copy link

Original issue created by sebastian.jancke on 2009-08-11 at 08:53 AM


There is a nice Iterables.getLast(..) method, however an
Iterables.getFirst(..) method would be also nice (that means also on
Iterators.class of course).

Currently you have to call getElement(.., 0), which reads not very nice.

When adding this method, having an variation getFirstOrDefault(..) as
somewhere discussed here (Issue 150) for getLast(..) seems like a natural
addition.

See also: http://code.google.com/p/google-collections/issues/detail?id=150

@gissuebot
Copy link
Author

Original comment posted by kevinb9n on 2009-08-11 at 01:26 PM


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().

@gissuebot
Copy link
Author

Original comment posted by sebastian.jancke on 2009-08-12 at 07:20 AM


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, ...).

@gissuebot
Copy link
Author

Original comment posted by sebastian.jancke on 2009-08-12 at 07:24 AM


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.

@gissuebot
Copy link
Author

Original comment posted by kevinb9n on 2009-08-12 at 05:01 PM


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!

@gissuebot
Copy link
Author

Original comment posted by kevinb9n on 2009-08-12 at 05:55 PM


(No comment entered for this change.)


Status: Accepted
Labels: post-1.0

@gissuebot
Copy link
Author

Original comment posted by sebastian.jancke on 2009-08-14 at 08:37 AM


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).

@gissuebot
Copy link
Author

Original comment posted by jim.andreou on 2009-08-14 at 10:08 AM


.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.

@gissuebot
Copy link
Author

Original comment posted by kevinb9n on 2009-09-17 at 05:01 PM


(No comment entered for this change.)

@gissuebot
Copy link
Author

Original comment posted by kevinb9n on 2009-09-17 at 05:45 PM


(No comment entered for this change.)


Labels: -post-1.0, Milestone-Post1.0

@gissuebot
Copy link
Author

Original comment posted by kevinb9n on 2009-09-17 at 05:57 PM


(No comment entered for this change.)


Labels: Type-Enhancement

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2010-03-16 at 08:04 PM


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.

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2010-07-30 at 03:50 AM


(No comment entered for this change.)


Labels: Milestone-Release07

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2010-08-30 at 05:33 PM


Looks like we've done this get, getLast and getOnlyElement but just need to do it for find and we're done.

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2010-08-30 at 05:39 PM


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.

@gissuebot
Copy link
Author

Original comment posted by boppenheim@google.com on 2010-08-30 at 05:59 PM


(No comment entered for this change.)


Owner: boppenh...@google.com

@gissuebot
Copy link
Author

Original comment posted by boppenheim@google.com on 2010-09-04 at 05:38 AM


Committed in r105.


Status: Fixed

@gissuebot
Copy link
Author

Original comment posted by ogregoire on 2010-11-23 at 02:38 PM


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.

@gissuebot
Copy link
Author

Original comment posted by ori.schwartz on 2011-12-22 at 08:43 PM


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.

@gissuebot
Copy link
Author

Original comment posted by ipremraj11 on 2011-12-24 at 10:01 AM


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.

@gissuebot
Copy link
Author

gissuebot commented Nov 1, 2014

Original comment posted by mar...@aie.pl on 2013-05-16 at 08:09 PM


How about returning an Optional on getFirst?

public static <T> Optional<T> getFirst(Iterable<T> i)

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2013-05-22 at 04:22 AM


We did that for FluentIterable. I doubt it's worth going back and fixing Iterables now. Use FluentIterable!

@gissuebot gissuebot added status=fixed migrated type=enhancement Make an existing feature better labels Nov 1, 2014
@cgdecker cgdecker modified the milestone: 7.0 Nov 1, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status=fixed type=enhancement Make an existing feature better
Projects
None yet
Development

No branches or pull requests

2 participants