peek() Method

The peek() method returns an object from the top of the stack. For example,


\begin{lstlisting}
import java.util.Stack;
\par
class Main {
public static void...
...;
System.out.println(''Element at top: '' + element);
\par
}
}
\end{lstlisting}

Output


\begin{lstlisting}
Stack: [Dog, Horse, Cat]
Element at top: Cat
\end{lstlisting}