Stack addAll(Collection) method in Java with Example
The addAll(Collection) method of Stack Class is used to append all of the elements from the collection passed as a parameter to this function to the end of a Stack keeping in mind the order of return by the collection’s iterator.
Syntax:
boolean addAll(Collection C)
Parameters: The method accepts a mandatory parameter C which is a collection of ArrayList. It is the collection whose elements are needed to be appended at the end of the Stack.
Return Value: The method returns True if at least one action of append is performed, else False.
Below program illustrate the Java.util.Stack.addAll() method:
// Java code to illustrate boolean addAll() import java.util.*; import java.util.ArrayList; public class GFG { public static void main(String args[]) { // Creating an empty Stack Stack<String> stack = new Stack<String>(); // Use add() method to add elements in the Stack stack.add("Geeks"); stack.add("for"); stack.add("Geeks"); stack.add("10"); stack.add("20"); // A collection is created Collection<String> c = new ArrayList<String>(); c.add("A"); c.add("Computer"); c.add("Portal"); c.add("for"); c.add("Geeks"); // Displaying the Stack System.out.println("The Stack is: " + stack); // Appending the collection to the Stack stack.addAll(c); // Clearing the Stack using clear() and displaying System.out.println("The new Stack is: " + stack); } } |
The Stack is: [Geeks, for, Geeks, 10, 20] The new Stack is: [Geeks, for, Geeks, 10, 20, A, Computer, Portal, for, Geeks]
Example 2:
// Java code to illustrate // boolean add(Object element) import java.util.*; public class StackDemo { public static void main(String args[]) { // Creating an empty Stack Stack<Integer> stack = new Stack<Integer>(); // Use add() method // to add elements in the Stack stack.add(10); stack.add(20); stack.add(30); stack.add(40); stack.add(50); // A collection is created Collection<Integer> c = new ArrayList<Integer>(); c.add(1); c.add(2); c.add(3); // Displaying the Stack System.out.println("The Stack is: " + stack); // Appending the collection to the Stack stack.addAll(c); // Clearing the Stack using clear() and displaying System.out.println("The new Stack is: " + stack); } } |
The Stack is: [10, 20, 30, 40, 50] The new Stack is: [10, 20, 30, 40, 50, 1, 2, 3]
Recommended Posts:
- Stack get() method in Java with Example
- Stack set() method in Java with Example
- Stack contains() method in Java with Example
- Stack pop() Method in Java
- Stack hashCode() method in Java with Example
- Stack peek() Method in Java
- Stack push() Method in Java
- Stack iterator() method in Java with Example
- Stack lastIndexOf() method in Java with Example
- Stack add(Object) method in Java with Example
- Stack addElement(E) method in Java with Example
- Stack elements() method in Java with Example
- Stack empty() Method in Java
- Stack add(int, Object) method in Java with Example
- Stack firstElement() method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




