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
Comments
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 |
Original comment posted by sebastian.jancke on 2009-08-12 at 07:20 AM
Calling iterable.iterator().next() is simply not readable - sorry. It is failing in |
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 |
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 Also, your notion of consistency is deeply misguided. We use consistency in how we |
Original comment posted by kevinb9n on 2009-08-12 at 05:55 PM (No comment entered for this change.) Status: |
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 I agree on not adding worthless functionality. Maybe highest readability is not a Still, having getFirst ist consistent with having getLast. It was the first thing I In the end - It was just a suggestion and expression of my experience with this very |
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 |
Original comment posted by kevinb9n on 2009-09-17 at 05:01 PM (No comment entered for this change.) |
Original comment posted by kevinb9n on 2009-09-17 at 05:45 PM (No comment entered for this change.) Labels: - |
Original comment posted by kevinb9n on 2009-09-17 at 05:57 PM (No comment entered for this change.) Labels: |
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 |
Original comment posted by kevinb@google.com on 2010-07-30 at 03:50 AM (No comment entered for this change.) Labels: |
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. |
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. |
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 |
Original comment posted by boppenheim@google.com on 2010-09-04 at 05:38 AM Committed in r105. Status: |
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:
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 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. |
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. |
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) |
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! |
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
The text was updated successfully, but these errors were encountered: