Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Get rid of the famous get(0) and get(list.size() -1)

User_PCUYROct 31 2021 — edited Nov 1 2021

Hello everybody.
After some use of the Collection API, i have a little idea to suggest for the manipulation of the lists.
I've encountered a lot of use-case where i need to get the first element (and maybe the last).
For now, the solution to get the first element in a list (object which implements the interface List) is the famous get(0) .. .. ..
I don't know for you but for me, with the fact that Java become more and more verbose, it gives me a lot of itches to write:
myList.get(0) to get the first element.
myList.get(myList.size - 1) to get the last element.
Today, for a better understanding of the logic, a better readability, how about having methods like:
myList.getFirst() for the first element.
myList.getLast() for the last element.
The advantage is not only on the meaning of the method but also to hide the unnecessary presence of the item's number (and by the way the compute of the last item's position).
I want to know your point of view about this idea.
Thanks.

Comments
Post Details
Added on Oct 31 2021
3 comments
510 views