Export to GitHub

google-collections - issue #217

Itera*s.getFirst(Iterator<T>, T) - and add the ", T" overloads for other Iter*s methods


Posted on Aug 11, 2009 by Grumpy Rabbit

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

Comment #1

Posted on Aug 11, 2009 by Grumpy Monkey

getFirst(Iterator, 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().

Comment #2

Posted on Aug 12, 2009 by Grumpy Rabbit

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

Comment #3

Posted on Aug 12, 2009 by Grumpy Rabbit

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.

Comment #4

Posted on Aug 12, 2009 by Grumpy Monkey

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!

Comment #5

Posted on Aug 12, 2009 by Grumpy Monkey

(No comment was entered for this change.)

Comment #6

Posted on Aug 14, 2009 by Grumpy Rabbit

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

Comment #7

Posted on Aug 14, 2009 by Massive Monkey

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

Comment #8

Posted on Sep 17, 2009 by Grumpy Monkey

(No comment was entered for this change.)

Comment #9

Posted on Sep 17, 2009 by Grumpy Monkey

(No comment was entered for this change.)

Comment #10

Posted on Sep 17, 2009 by Grumpy Monkey

(No comment was entered for this change.)

Comment #11

Posted on Jan 5, 2010 by Happy Wombat

This issue has been moved to the Guava project (keeping the same id number). Simply replace 'google-collections' with 'guava-libraries' in your address bar and it should take you there.

Comment #12

Posted on Apr 16, 2011 by Grumpy Wombat

Wecom e to the good website:

http://www.shoppinglouisvuitton.org louis vuitton outlet
http://www.tiffanyjewelleryshop.org tiffany necklace
http://www.ghdstraightenerssale.com ghd straighteners sale http://www.christianlouboutinshopping.org christian louboutin
http://www.maccosmeticsshop.org mac eyeshadow
http://www.mbtshoesshopping.com mbt shoes sale
http://www.sunglassesshopping.org cheap oakley sunglass
http://www.handbagshopping.org chanel handbags for sale
http://www.christianlouboutinonline.net christian louboutin boots
http://www.chanelbagonline.net chanel handbag http://www.guccigucci.org discount gucci handbags

Status: Moved

Labels:
Milestone-Post1.0 Type-Enhancement