The java.util.Stack.search(Object element) method in Java is used to search for an element in the stack and get its distance from the top. This method starts the count of the position from 1 and not from 0. The element that is on the top of the stack is considered to be at position 1. If more than one element is present, the index of the element closest to the top is returned. The method returns its position if the element is successfully found and returns -1 if the element is absent.
Syntax:
STACK.search(element)
Parameters: The method accepts one parameter element which refers to the element that is required to be searched for in the Stack.
Return Value: The method returns the position of the element if it is successfully found in the stack (taking the count as base 1) else -1 is returned.
Below programs illustrate the working of java.util.Stack.search() method:
Program 1:
// Java code to demonstrate search() method import java.util.*; public class Stack_Demo { public static void main(String[] args) { // Creating an empty Stack Stack<String> STACK = new Stack<String>(); // Stacking strings STACK.push("Geeks"); STACK.push("4"); STACK.push("Geeks"); STACK.push("Welcomes"); STACK.push("You"); // Displaying the Stack System.out.println("The stack is: " + STACK); // Checking for the element "4" System.out.println("Does the stack contains '4'? " + STACK.search("4")); // Checking for the element "Hello" System.out.println("Does the stack contains 'Hello'? " + STACK.search("Hello")); // Checking for the element "Geeks" System.out.println("Does the stack contains 'Geeks'? " + STACK.search("Geeks")); } } |
The stack is: [Geeks, 4, Geeks, Welcomes, You] Does the stack contains '4'? 4 Does the stack contains 'Hello'? -1 Does the stack contains 'Geeks'? 3
Program 2:
// Java code to demonstrate search() method import java.util.*; public class Stack_Demo { public static void main(String[] args) { // Creating an empty Stack Stack<Integer> STACK = new Stack<Integer>(); // Stacking int values STACK.push(8); STACK.push(5); STACK.push(9); STACK.push(2); STACK.push(4); // Displaying the Stack System.out.println("The stack is: " + STACK); // Checking for the element 9 System.out.println("Does the stack contains '9'? " + STACK.search(9)); // Checking for the element 10 System.out.println("Does the stack contains '10'? " + STACK.search(10)); // Checking for the element 11 System.out.println("Does the stack contains '11'? " + STACK.search(11)); } } |
The stack is: [8, 5, 9, 2, 4] Does the stack contains '9'? 3 Does the stack contains '10'? -1 Does the stack contains '11'? -1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Stack get() method in Java with Example
- Stack contains() method in Java with Example
- Stack pop() Method in Java
- Stack set() method in Java with Example
- Stack toString() method in Java with Example
- Stack listIterator(int) method in Java with Example
- Stack elementAt() method in Java with Example
- Stack add(Object) method in Java with Example
- Stack size() method in Java with Example
- Stack add(int, Object) method in Java with Example
- Stack subList() method in Java with Example
- Stack toArray() method in Java with Example
- Stack removeAllElements() method in Java with Example
- Stack setSize() method in Java with Example
- Stack remove(int) method in Java with Example
- Stack removeElementAt() method in Java with Example
- Stack push() Method in Java
- Stack listIterator() method in Java with Example
- Stack toArray(T[]) method in Java with Example
- Stack empty() 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.
Improved By : sharmahs

