Java Stack search() Method



Description

The search(Object o) method is used to return the 1-based position where an object is on this stack.

Declaration

Following is the declaration for java.util.Stack.search() method.

public int search(Object o)

Parameters

o − This is the desired object.

Return Value

The method call returns the 1-based position from the top of the stack where the object is located.

Exception

NA

Search an Integer in Stack Example

The following example shows the usage of Java Stack search() method to search an integer in the stack using 1 based index In this example, we've created a Stack object of Integers. Then we've added few integers to the stack and printed the index of searched element using search() method.

package com.tutorialspoint;

import java.util.Stack;

public class StackDemo {
   public static void main(String args[]) {

      // creating stack
      Stack<Integer> st = new Stack<>();

      // populating stack
      st.push(10);
      st.push(20);
      st.push(30);

      // searching 30 in the stack
      System.out.println("Searching 30 in stack: "+st.search(30));
   }    
}

Let us compile and run the above program, this will produce the following result.

Searching 30 in stack: 1

Search a String in Stack Example

The following example shows the usage of Java Stack search() method to search a String in the stack using 1 based index In this example, we've created a Stack object of Strings. Then we've added few strings to the stack and printed the index of searched string using search() method.

package com.tutorialspoint;

import java.util.Stack;

public class StackDemo {
   public static void main(String args[]) {

      // creating stack
      Stack<String> st = new Stack<>();

      // populating stack
      st.push("Java");
      st.push("Source");
      st.push("code");

      // searching 'code' element
      System.out.println("Searching 'code' in stack: "+st.search("code"));
   }    
}

Let us compile and run the above program, this will produce the following result.

Searching 'code' in stack: 1

Search an Object in Stack Example

The following example shows the usage of Java Stack search() method to search an object in the stack using 1 based index In this example, we've created a Stack of Student objects. Then we've added few students to the stack and printed the index of searched student using search() method.

package com.tutorialspoint;

import java.util.Stack;

public class StackDemo {
   public static void main(String args[]) {

      // creating stack
      Stack<Student> st = new Stack<>();

      // populating stack
      st.push(new Student(1, "Julie"));
      st.push(new Student(2, "Robert"));
      st.push(new Student(3, "Adam"));

      // searching 'Adam'
      System.out.println("Searching 'Adam' in stack: "+st.search(new Student(3, "Adam")));
   }    
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   // equals method is overridden so that we can search the student using
   // our custom logic
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Let us compile and run the above program, this will produce the following result.

Searching 'Adam' in stack: 1
java_util_stack.htm
Advertisements