import java.util.LinkedList; public class ExampleLinkedList { public static void main(String[] args) { LinkedListlist = new LinkedList<>(); list.add("apple"); list.add("banana"); list.addFirst("orange"); System.out.println(list); } } //Output: [orange, apple, banana]
import java.util.LinkedList; public class ExampleLinkedList { public static void main(String[] args) { LinkedListIn this example, we use the addFirst method to create a simple Stack using a LinkedList. We add elements to the beginning of the LinkedList using addFirst(), which allows us to remove them later in a stack-like fashion using the pop() method. The java.util package library contains various classes and interfaces that provide basic functionality to collections like LinkedList, ArrayList, HashSet, TreeMap, and HashSet.stack = new LinkedList<>(); stack.addFirst("apple"); stack.addFirst("banana"); stack.addFirst("orange"); System.out.println(stack); stack.pop(); System.out.println(stack); } } //Output: //[orange, banana, apple] //[banana, apple]