Stack Examples
- Creating a Stack and Performing Basic Operations like Push, Pop and Peek
- Java Stack push() Method Example
- Iterating over a Stack Example
- Java Stack pop() Method Example
- Java Stack peek() Method Example
- Java Stack empty() Method Example
- Java Stack search() Method Example
- Java Convert ArrayList to Stack Example
- Java Convert Array to Stack Example
This example demonstrates how to iterate over an ArrayList and push each element into Stack.
Java Convert ArrayList to Stack Example
import java.util.ArrayList; import java.util.List; import java.util.Stack; /** * Class demonstrates the usage of Stack class methods with examples * * @author sourcecodeexamples.net * */ public class ListToStackExample { public static void main(String[] args) { convertListToStack(); } private static void convertListToStack() { Stack < String > stack = new Stack < > (); List < String > list = new ArrayList < > (); list.add("Java"); list.add("JEE"); list.add("C"); list.add("C++"); list.add("Spring"); list.add("Hibernate"); System.out.println("Non-Empty stack addAll Operation : " + stack.addAll(list)); System.out.println("Non-Empty stack : " + stack); } }
Reference
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course