Stack set() method in Java with Example Last Updated : 24 Dec, 2018 Comments Improve Suggest changes Like Article Like Report The set() method of Java Stack is used to replace any particular element in the stack created using the Stack class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() method. Syntax: public E set(int index, Object element) Parameters: This function accepts two parameters as shown in the above syntax and described below. index: This is of integer type and refers to the position of the element that is to be replaced from the stack. element: It is the new element by which the existing element will be replaced and is of the same object type as the stack. Return Value: The method returns the previous value from the stack that is replaced with the new value. Exception: This method throws following exceptions: UnsupportedOperationException: if the set operation is not supported by this stack ClassCastException: if the class of the specified element prevents it from being added to this stack NullPointerException: if the specified element is null and this stack does not permit null elements IllegalArgumentException: if some property of the specified element prevents it from being added to this stack IndexOutOfBoundsException: if the index is out of range (index = size()) Below program illustrate the Java.util.Stack.set() method: Example 1: Java // Java code to illustrate set() import java.io.*; import java.util.*; public class StackDemo { 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"); // Displaying the linkedstack System.out.println("Stack:" + stack); // Using set() method to replace Geeks with GFG System.out.println("The Object that is replaced is: " + stack.set(2, "GFG")); // Using set() method to replace 20 with 50 System.out.println("The Object that is replaced is: " + stack.set(4, "50")); // Displaying the modified linkedstack System.out.println("The new Stack is:" + stack); } } Output: Stack:[Geeks, for, Geeks, 10, 20] The Object that is replaced is: Geeks The Object that is replaced is: 20 The new Stack is:[Geeks, for, GFG, 10, 50] Example 2: To demonstrate IndexOutOfBoundException Java // Java code to illustrate set() import java.io.*; import java.util.*; public class StackDemo { 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"); // Displaying the linkedstack System.out.println("Stack:" + stack); // Using set() method to replace 10th with GFG // and the 10th element does not exist System.out.println("Trying to replace 10th " + "element with GFG"); try { stack.set(10, "GFG"); } catch (Exception e) { System.out.println(e); } } } Output: Stack:[Geeks, for, Geeks, 10, 20] Trying to replace 10th element with GFG java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 10 Comment More infoAdvertise with us Next Article Stack set() 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 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 Stack get() method in Java with Example The Java.util.Stack.get() method is used to fetch or retrieve an element at a specific index from a Stack. Syntax: Stack.get(int index) Parameters: This method accepts a mandatory parameter index which is of integer data type. It specifies the position or index of the element to be fetched from the 2 min read Stack subList() method in Java with Example The subList() method of Java.util.Stack class is used to return a view of the portion of this Stack between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned Stack is empty.) The returned Stack is backed by this Stack, so non-structural cha 3 min read 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 2 min read 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 man 2 min read Stack toArray() method in Java with Example The toArray() method of Stack class in Java is used to form an array of the same elements as that of the Stack. Basically, it copies all the element from a Stack to a new array. Syntax: Object[] arr = Stack.toArray() Parameters: The method does not take any parameters. Return Value: The method retur 2 min read Stack setSize() method in Java with Example The setSize() method of Java.util.Stack class changes the size of this Stack instance to the size passed as the parameter. Syntax: public void setSize(int size) Parameters: This method takes the new size as a parameter. Exception: This method throws ArrayIndexOutOfBoundsException if the new size is 2 min read Like