java list get first element
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
3.5
4
list.get(0);
Thank you!
4
0
0
3
import java.util.ArrayList;
public class FirstandLastElemets{
public static void main(String[] args){
ArrayList<String> list = new ArrayList<String>();
//Instantiating an ArrayList object
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);
//Removing the sub 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));
}
}
Thank you!
3
0
0
0
!list_or_set.isEmpty()
Thank you!
0
0
3.8
10
set.iterator().next();
Thank you!
10
0
Are there any code examples left?
New code examples in category Java