Stack equals() method in Java with Example
The Java.util.Stack.equals(Object obj) method of Stack class in Java is used verify the equality of an Object with a Stack and compare them. The list returns true only if both Stack contains same elements with same order.
Syntax:
first_Stack.equals(second_Stack)
Parameters: This method accepts a mandatory parameter second_Stack which refers to the second Stack to be compared to the first Stack.
Return value: The method returns true if the equality holds and both the objects and Stack are equal else it returns false.
Below programs are used to illustrate the working of the java.util.Stack.elements() method:
Program 1:
// Java code to illustrate the equals() method import java.util.*; public class Stack_Demo { public static void main(String[] args) { // Creating an empty Stack Stack<String> stack1 = new Stack<String>(); // Inserting elements into the table stack1.add("Geeks"); stack1.add("4"); stack1.add("Geeks"); stack1.add("Welcomes"); stack1.add("You"); // Displaying the Stack System.out.println("The Stack is: " + stack1); // Creating an empty Stack Stack<String> stack2 = new Stack<String>(); // Inserting elements into the table stack2.add("Geeks"); stack2.add("4"); stack2.add("Geeks"); stack2.add("Welcomes"); stack2.add("You"); // Displaying the Stack System.out.println("The Stack is: " + stack2); System.out.println("Are both of them equal? " + stack1.equals(stack2)); } } |
The Stack is: [Geeks, 4, Geeks, Welcomes, You] The Stack is: [Geeks, 4, Geeks, Welcomes, You] Are both of them equal? true
Program 2 :
// Java code to illustrate the equals() method import java.util.*; public class Stack_Demo { public static void main(String[] args) { // Creating an empty Stack Stack<Integer> stack1 = new Stack<Integer>(); // Inserting elements into the table stack1.add(10); stack1.add(15); stack1.add(20); stack1.add(25); stack1.add(30); // Displaying the Stack System.out.println("The Stack is: " + stack1); // Creating an empty Stack Stack<Integer> stack2 = new Stack<Integer>(); // Inserting elements into the table stack2.add(10); stack2.add(15); stack2.add(20); stack2.add(25); stack2.add(30); stack2.add(40); // Displaying the Stack System.out.println("The Stack is: " + stack2); System.out.println("Are both of them equal? " + stack1.equals(stack2)); } } |
The Stack is: [10, 15, 20, 25, 30] The Stack is: [10, 15, 20, 25, 30, 40] Are both of them equal? false
Recommended Posts:
- HashSet equals() method in Java with Example
- YearMonth equals() method in Java
- Overriding equals method in Java
- MathContext equals() Method in Java
- ConcurrentLinkedDeque equals() method in Java with Example
- GregorianCalendar equals() Method in Java
- DecimalFormat equals() method in Java
- AbstractSequentialList equals() method in Java with Example
- LinkedBlockingDeque equals() method in Java with Example
- Vector equals() Method in Java
- TreeSet equals() method in Java with Example
- IntBuffer equals() method in Java
- IdentityHashMap equals() Method in Java
- StringWriter equals() method in Java with Example
- CopyOnWriteArraySet equals() method in Java
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.



