0% found this document useful (0 votes)
7 views2 pages

Java Stack Methods

The Java Stack class, part of the java.util package, implements a last-in, first-out (LIFO) stack of objects. It includes methods such as push, pop, peek, isEmpty, search, and size for interacting with the stack. Each method has a specific function, such as adding, removing, or checking items in the stack.

Uploaded by

relishmunjal07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Java Stack Methods

The Java Stack class, part of the java.util package, implements a last-in, first-out (LIFO) stack of objects. It includes methods such as push, pop, peek, isEmpty, search, and size for interacting with the stack. Each method has a specific function, such as adding, removing, or checking items in the stack.

Uploaded by

relishmunjal07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Stack Class - Methods

Java's Stack class (from the java.util package) is a collection class that implements a

last-in, first-out (LIFO) stack of objects. It provides various methods for interacting

with the stack, as listed below:

1. push(E item):

- Pushes an item onto the top of the stack.

- Syntax: stack.push(item);

2. pop():

- Removes and returns the top item of the stack.

- Syntax: E item = stack.pop();

3. peek():

- Returns the top item of the stack without removing it.

- Syntax: E item = stack.peek();

4. isEmpty():

- Checks if the stack is empty.

- Syntax: boolean isEmpty = stack.isEmpty();

5. search(Object o):

- Returns the 1-based position of the element from the top of the stack.

- Returns -1 if the item is not found.

- Syntax: int position = stack.search(o);


6. size():

- Returns the number of elements in the stack.

- Syntax: int size = stack.size();

You might also like