Java Stack search() Method

The search(Object o) method in Java, part of the java.util.Stack class, is used to search for an object in the stack and return its 1-based position from the top of the stack. This method is useful for finding the position of an element within the stack.

Table of Contents

  1. Introduction
  2. search(Object o) Method Syntax
  3. Understanding search(Object o)
  4. Examples
    • Basic Usage
    • Handling Non-Existent Elements
  5. Real-World Use Case
  6. Conclusion

Introduction

The search(Object o) method returns the 1-based position of the object from the top of the stack. If the object is found, its position is returned; otherwise, -1 is returned.

search(Object o) Method Syntax

The syntax for the search(Object o) method is as follows:

public int search(Object o)

Parameters:

  • o: The object to search for in the stack.

Returns:

  • The 1-based position of the object from the top of the stack, or -1 if the object is not found.

Throws:

  • No exceptions are thrown by this method.

Understanding search(Object o)

The search(Object o) method searches the stack for the specified object. The position returned is 1-based, meaning the top element of the stack is considered to be at position 1. If the object is not found, the method returns -1.

Examples

Basic Usage

To demonstrate the basic usage of search(Object o), we will create a Stack object, push some elements onto the stack, and then search for an element.

Example

import java.util.Stack;

public class SearchExample {
    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();

        // Push elements onto the stack
        stack.push("apple");
        stack.push("banana");
        stack.push("cherry");

        // Search for an element
        int position = stack.search("banana");
        System.out.println("Position of 'banana': " + position);
    }
}

Output:

Position of 'banana': 2

Handling Non-Existent Elements

This example shows how to handle the situation when the element being searched for does not exist in the stack.

Example

import java.util.Stack;

public class SearchNonExistentExample {
    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();

        // Push elements onto the stack
        stack.push("apple");
        stack.push("banana");
        stack.push("cherry");

        // Search for an element that does not exist
        int position = stack.search("date");
        System.out.println("Position of 'date': " + position);
    }
}

Output:

Position of 'date': -1

Real-World Use Case

Checking Recent Actions in a History Stack

In real-world applications, the search(Object o) method can be used to check if a recent action is present in a history stack and its position from the top.

Example

import java.util.Stack;

public class ActionHistory {
    public static void main(String[] args) {
        Stack<String> history = new Stack<>();

        // User performs actions
        history.push("open file");
        history.push("edit file");
        history.push("save file");

        // Check if a specific action is in the history
        int position = history.search("edit file");
        if (position != -1) {
            System.out.println("'edit file' action is " + position + " steps from the top of the history.");
        } else {
            System.out.println("'edit file' action is not in the history.");
        }
    }
}

Output:

'edit file' action is 2 steps from the top of the history.

Conclusion

The Stack.search(Object o) method is used to search for an object in the stack and return its 1-based position from the top of the stack. This method is particularly useful for determining the position of an element within the stack. By understanding and using this method, you can efficiently manage and query stack-based data structures in your Java applications.

Comments