This example will show how to get the first element in a collection. While this may seem trivial, utilities exist to provide a more readable, eloquent approach vs the solution outlined in the straight up java section.
Straight up Java
This snippet will get the first element in a collection using java. It will check if the ArrayList is empty and if the size is greater than 0. It then will return the first element by getting the first element in the list.
This snippet will find the first element in arraylist using java 8. The Java 8 Streams API provides Streams.findFirst which will return the wrapper Optional object describing the first element of the stream. If the arraylist is empty, an empty Optional will be returned.
Guava Iterables.getFirst will return the first element in the list or the default value if the list is empty.
Apache commons CollectionUtils.get will return the element at the specified index.