find first element of list in java

ht‮/:spt‬/www.lautturi.com
find first element of list in java
final Object firstElement = list.stream()
  .findFirst()
  .orElse(new Object()) //Give a default value if there are no elements
  
final Object firstElement = list.stream()
  .findFirst()
  .orElseThrow(() -> new Exception()) //Throw an exception if there are no elements
Created Time:2017-09-05 15:25:08  Author:lautturi