1final Object firstElement = list.stream()
2  .findFirst()
3  .orElse(new Object()) //Give a default value if there are no elements
4  
5final Object firstElement = list.stream()
6  .findFirst()
7  .orElseThrow(() -> new Exception()) //Throw an exception if there are no elements  1import java.util.ArrayList;
2public class FirstandLastElemets{
3   public static void main(String[] args){
4      ArrayList<String> list = new ArrayList<String>();
5      //Instantiating an ArrayList object
6      list.add("JavaFX");
7      list.add("Java");
8      list.add("WebGL");
9      list.add("OpenCV");
10      list.add("OpenNLP");
11      list.add("JOGL");
12      list.add("Hadoop");
13      list.add("HBase");
14      list.add("Flume");
15      list.add("Mahout");
16      list.add("Impala");
17      System.out.println("Contents of the Array List: \n"+list);
18      //Removing the sub list
19      System.out.println("First element of the array list: "+list.get(0));
20      System.out.println("Last element of the array list: "+list.get(list.size()-1));
21   }
22}