dir.by  
Поиск >>

Компьютер, программы
→
Java
→
Java (class, interface, collections, etc.)
 class Stack in Java 
посмотрели 930 раз
обновлено: 6 October 2026
class Stack<E>   extends class Vector<E> with the algorithm "last in - first out":
Each new element is placed at the very top.
The first element is the one at the very top.

Stack is fully synchronized (thread-safe).

Class Stack extends class Vector with five functions that allow a vector to be treated as a stack:
  Java  
push(E item) // Add a new element and the element is placed at the very top
E pop() // Pull out the element, take the element that is at the very top and delete it
E peek() // Look at the element that is at the very top, the element is not deleted
boolean empty() // checks if this stack is empty
int search(Object o) // Look for the item, the position is returned starting with 1
Example
  Java     Creating a new Java console application ... and write the code
package org.example;
import java.util.Stack;

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

          // Add the element to the top
          myList1.push("Car");
          // myList1 = ["Car"]

          // Add the element to the top
          myList1.push("House");
          // myList1 = ["Car", "House"]

          // Add the element to the top
          myList1.push("Book");
          // myList1 = ["Car", "House", "Book"]

          // View item at the top
          String elem = myList1.peek();
          // elem = "Book"
          // myList1 = ["Car", "House", "Book"]

          // Take the element from the top, the element is deleted
          elem = myList1.pop();
          // elem = "Book"
          // myList1 = ["Car", "House"]

          /////// can call Vector functions /////////

          // Add an element to the end
          myList1.add("Notebook");
          // myList1 = ["Car", "House", "Notebook"]

          // View item by index
          elem = myList1.get(1);
          // elem = "House"
          // myList1 = ["Car", "House", "Notebook"]

          // Insert element by index 1
          myList1.add(1, "Airplane");
          // myList1 = ["Car", "Airplane", "House", "Notebook"]

          // Delete item by index
          myList1.remove(1);
          // myList1 = ["Car", "House", "Notebook"]
     }
}
  Kotlin     Creating a new Kotlin console application ... and write the code
import java.util.Stack

fun main() {

     val myList1 = Stack<String>()

     // Add the element to the top
     myList1.push("Car")
     // myList1 = ["Car"]

     // Add the element to the top
     myList1.push("House")
     // myList1 = ["Car", "House"]

     // Add the element to the top
     myList1.push("Book")
     // myList1 = ["Car", "House", "Book"]

     // View item at the top
     var elem = myList1.peek()
     // elem = "Book"
     // myList1 = ["Car", "House", "Book"]

     // Take the element from the top, the element is deleted
     elem = myList1.pop()
     // elem = "Book"
     // myList1 = ["Car", "House"]

     /////// can call Vector functions /////////

     // Add an element to the end
     myList1.add("Notebook")
     // myList1 = ["Car", "House", "Notebook"]

     // View item by index
     elem = myList1[1]
     // elem = "House"
     // myList1 = ["Car", "House", "Notebook"]

     // Insert element by index 1
     myList1.add(1, "Airplane")
     // myList1 = ["Car", "Airplane", "House", "Notebook"]

     // Delete item by index
     myList1.removeAt(1)
     // myList1 = ["Car", "House", "Notebook"]
}
Documentation
  Java  
class Stack<E> extends Vector<E>
{
     public Stack() {
     }

     public E push(E item) {
          addElement(item);

          return item;
     }

     public synchronized E pop() {
          E obj;
          int len = size();

          obj = peek();
          removeElementAt(len - 1);

          return obj;
     }

     public synchronized E peek() {
          int len = size();

          if (len == 0)
               throw new EmptyStackException();
          return elementAt(len - 1);
     }

     public boolean empty() {
          return size() == 0;
     }

     public synchronized int search(Object o) {
          int i = lastIndexOf(o);

          if (i >= 0) {
               return size() - i;
          }
          return -1;
     }
}
Learn more about methods in the Java documentation:https://docs.oracle.com ...
 
← Previous topic
class Vector<E> in Java
 
Next topic →
class CopyOnWriteArrayList<E> in Java
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

 
Download and install
• Installation JDK (download and install the Java library for Windows)
• Download and install IntelliJ IDEA to explore Java, Spring, Jakarta EE | Functionality and differences: Community | Ultimate
• Error "JDK is missing" | Java
Java (class, interface, collections, etc.)
• Create a simple console app in IntelliJ IDEA to learn Java
• Access modifiers (private, protected, public) for a class and for methods and fields. | Java
• Collections in Java
• interface List<E> in Java
• class ArrayList<E> in Java
• class Vector<E> in Java
• class Stack<E> in Java
• class CopyOnWriteArrayList<E> in Java
• interface Queue<E> in Java
• interface Deque<E> in Java
• class LinkedList<E> in Java
• interface Set in Java
• class HashSet<E> in Java
• class LinkedHashSet<E> in Java
• Collections.singletonList("Hello") returns an immutable list containing exactly one specified element | Java collection
• class Collections (all functions, algorithms) | Java
• stream.filter(...) select elements according to your criterion | Java collection | Example: allCars.stream().filter(item -> item.Price < 30000).toList()
• What is interface in Java ?
• Directly in the code (at runtime) we make an implementation for the interface | Java
• Threads in Java
• lambda is an unnamed method (no name and no class owned) | Java
• Exceptions and handling in Java. Example: try {...} catch (Exception e) {...}
• Why use throws Exception in the function name in Java? Answer: The method throws an exception and there is no handling of try catch. Example: float CalculateDensity(float mass, float volume) throws Exception { ... throw new Exception("Error! Volume is zero."); ... }
Java web application (servlet, jsp page, basic functionality and no frameworks)
• Creating a new simple web application (jsp web page) | Java, Maven
• Create a web servlet (the servlet is located on the web server, returns the result on request) | Java
• Installation Tomcat web server (download and install for Windows)
• Check, test Tomcat web server (create a new my.hml file)
• What is Maven
• What is a dynamic web page jsp (Java Server Page) ?
• How to install Smart Tomcat plugin in Intellij Idea
• Error "The SDK is not specified for module ... | Project SDK is not defined ..." in IntelliJ IDEA | Java
Spring in Java (Spring Framework, Spring Data, Spring Boot, ...)
• What is Spring? Why use Spring in Java?
• What is Spring Bean? What is Spring Container? IoC (inversion of control) | Creating a simple Spring project and configuration Spring Container in an XML file | Editor IntelliJ Idea and language Java
• What is Dependency Injection in Spring? Creating a simple Spring project with Dependency Injection in the constructor with the attributes @Component, @Autowired | Editor IntelliJ Idea and language Java
• Create a new Spring Web application (the request goes to the Controller → Controller creates a response as text or json) | Java, Spring Web, Spring Boot, Maven
• Creating a new Spring WebSocket application (Java WebSocket sends a message to the JavaScript WebSocket) | Java, Spring WebSocket, Spring Boot, Maven
• Create Azure Web App with the type Java (free). That is, create an empty web server
• Put the Java Spring web application in Azure
• Connect to the application Azure Web App (Java) via FTP | use File Explorer
• Error "The SDK is not specified for module ... | Project SDK is not defined ..." in IntelliJ IDEA | Java, Spring Boot
• How to install Spring WebSocket plugin in Intellij Idea
Jakarta EE / Java EE in Java (Web Applications, Web Services)
• What is Jakarta EE (Java EE)? | In which editor (program) is it convenient to write Jakarta EE (Java EE) code?
• Creating a simple web project in IntelliJ Idea Ultimate | Jakarta EE (Java EE)
Struts in Java (extends API Java Servlet using MVC)
• What is Struts | Java
Courses, training
• Course "Java developer" from Yandex
Interview
• Interviews on Java

  Ваши вопросы присылайте по почте: info@dir.by