Why there is no getFirst(iterable) method?

后端 未结 2 1376
清歌不尽
清歌不尽 2021-01-03 17:42

Iterables present two methods for getLast

 public static  T getLast(Iterable iterable);
 public static  T getLast(It         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-03 18:16

    As an additions to @JohnB's answer I'd like to show Guava's devs opinion about getFirst(iterable). Kevin Bourrillion (head Guava's dev) writes there:

    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!

    So, you have a choice:

    • using iterable.iterator().next(),
    • using Iterables.getFirst(Iterable iterable, T default),
    • using Iterables.get(Iterable, 0),
    • writing your own method (probably containing iterable.iterator().next() and some docs) and use it as i.e. Iterables2.getFirst(iterable),
    • waiting for Kevin to change his mind ;)

    PS: I had similar doubt some time ago and found exact duplicate of this question at that time.

提交回复
热议问题