get first element of list java code example
Example 1: find first element of list java
final Object firstElement = list.stream()
.findFirst()
.orElse(new Object())
final Object firstElement = list.stream()
.findFirst()
.orElseThrow(() -> new Exception())
Example 2: java list get first element
list.get(0);
Example 3: java get first element from arraylist
import java.util.ArrayList;
public class FirstandLastElemets{
public static void main(String[] args){
ArrayList<String> list = new ArrayList<String>();
list.add("JavaFX");
list.add("Java");
list.add("WebGL");
list.add("OpenCV");
list.add("OpenNLP");
list.add("JOGL");
list.add("Hadoop");
list.add("HBase");
list.add("Flume");
list.add("Mahout");
list.add("Impala");
System.out.println("Contents of the Array List: \n"+list);
System.out.println("First element of the array list: "+list.get(0));
System.out.println("Last element of the array list: "+list.get(list.size()-1));
}
}
Example 4: java list get first element
!list_or_set.isEmpty()
Example 5: how to find first value of an list
list = [1,2,3]
firstdigit = list[0]
list = ['Hello', 'bye', 'Adios']
firstvalue = list[0]