import java.util.LinkedList; public class LinkedListExample { public static void main(String[] args) { LinkedListlist = new LinkedList<>(); list.add("apple"); list.add("banana"); list.add("cherry"); String first = list.pollFirst(); System.out.println("First element: " + first); // "First element: apple" } }
import java.util.LinkedList; public class LinkedListExample { public static void main(String[] args) { LinkedListIn this example, we create an empty LinkedList and try to remove the first element using pollFirst(). Since the list is empty, the method returns null and we print it to the console. The java.util package library provides various classes and interfaces to implement collections in Java.list = new LinkedList<>(); Integer first = list.pollFirst(); System.out.println("First element: " + first); // "First element: null" } }