Stack equals() method in Java with Example Last Updated : 24 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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 // 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)); } } Output: 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 // 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)); } } Output: The Stack is: [10, 15, 20, 25, 30] The Stack is: [10, 15, 20, 25, 30, 40] Are both of them equal? false Comment More infoAdvertise with us Next Article Stack equals() method in Java with Example C code_r Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-Stack +1 More Practice Tags : JavaJava-Collections Similar Reads Stack copyInto() method in Java with Example The java.util.Stack.copyInto() method is used to copy all of the components from this Stack to another Stack, having enough space to hold all of the components of the Stack. It is to be noted that the index of the elements remains unchanged. The elements present in the Stack are replaced by the elem 2 min read Stack elementAt() method in Java with Example The Java.util.Stack.elementAt(int pos) method is used to fetch or retrieve an element at a specific index from a Stack. Syntax: Stack.elementAt(int pos) Parameters: This method accepts a mandatory parameter pos of integer data type that specifies the position or index of the element to be fetched fr 2 min read Stack isEmpty() method in Java with Example The Java.util.Stack.isEmpty() method in Java is used to check and verify if a Stack is empty or not. It returns True if the Stack is empty else it returns False. Syntax: Stack.isEmpty() Parameters: This method does not take any parameter. Return Value: This function returns True if the Stackis empty 2 min read Stack insertElementAt() method in Java with Example The Java.util.Stack.insertElementAt(element, index) method is used to insert a particular element at the specified index of the Stack. Both the element and the position is passed as the parameters. If an element is inserted at a specified index, then all the elements are pushed upward by one and hen 2 min read Stack clear() method in Java with Example The Java.util.Stack.clear() method is used to remove all the elements from a Stack. Using the clear() method only clears all the element from the Stack and does not delete the Stack. In other words, we can say that the clear() method is used to only empty an existing Stack. Syntax: Stack.clear() Par 2 min read Stack indexOf(Object, int) method in Java with Example The Java.util.Stack.indexOf(Object element, int index) method is used to the index of the first occurrence of the specified element in this Stack, searching forwards from index, or returns -1 if the element is not found. More formally, returns the lowest index i such that (i >= index && O 2 min read Stack toArray(T[]) method in Java with Example The toArray(T[]) method method of Stack class in Java is used to form an array of the same elements as that of the Stack. It returns an array containing all of the elements in this Stack in the correct order; the run-time type of the returned array is that of the specified array. If the Stack fits i 4 min read Stack indexOf() method in Java with Example The Java.util.Stack.indexOf(Object element) method is used to check and find the occurrence of a particular element in the Stack. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the Stack does not contain the element. Syntax: St 2 min read Stack containsAll() method in Java with Example The containsAll() method of Java Stack is used to check whether two stacks contain the same elements or not. It takes one stack as a parameter and returns True if all of the elements of this stack is present in the other stack. Syntax: public boolean containsAll(Collection C) Parameters: The paramet 2 min read Stack removeRange() method in Java with Example The removeRange() method of Stack in Java is used to remove all elements within the specified range from an Stack object. It shifts any succeeding elements to the left. This call shortens the stack by (toIndex-fromIndex) elements where toIndex is the ending index and fromIndex is the starting index 2 min read Like