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:
import java.util.*;
import java.util.ArrayList;
public class GFG {
public static void main(String args[])
{
Stack<String> stack = new Stack<String>();
stack.add( "Geeks" );
stack.add( "for" );
stack.add( "Geeks" );
stack.add( "10" );
stack.add( "20" );
Collection<String> c = new ArrayList<String>();
c.add( "A" );
c.add( "Computer" );
c.add( "Portal" );
c.add( "for" );
c.add( "Geeks" );
System.out.println( "The Stack is: " + stack);
stack.addAll(c);
System.out.println( "The new Stack is: " + stack);
}
}
|
Output:
The Stack is: [Geeks, for, Geeks, 10, 20]
The new Stack is: [Geeks, for, Geeks, 10, 20, A, Computer, Portal, for, Geeks]
Example 2:
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
Stack<Integer> stack
= new Stack<Integer>();
stack.add( 10 );
stack.add( 20 );
stack.add( 30 );
stack.add( 40 );
stack.add( 50 );
Collection<Integer> c = new ArrayList<Integer>();
c.add( 1 );
c.add( 2 );
c.add( 3 );
System.out.println( "The Stack is: " + stack);
stack.addAll(c);
System.out.println( "The new Stack is: " + stack);
}
}
|
Output:
The Stack is: [10, 20, 30, 40, 50]
The new Stack is: [10, 20, 30, 40, 50, 1, 2, 3]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Dec, 2018
Like Article
Save Article