Java Stack peek() method

Java Stack peek() method

peek() method get the top element of this stack without removing it from the stack.

/**
 * @author lautturi.com 
 * Java example:Stack class peek() method in Java
 */

import java.util.*;
import java.util.regex.Pattern;

public class Lautturi {
	public static void main(String[] args){
		
		Stack<Character> stack = new Stack<Character>();
		
		stack.push('A');
		stack.push('B');
		stack.push('C');
		stack.push('D');

		System.out.println(stack);
		Character ch = stack.peek();
        System.out.println("Element at top: " + ch);
		
	}
}
Sour‮al.www:ec‬utturi.com

output:

[A, B, C, D]
Element at top: D
Created Time:2017-09-30 15:06:06  Author:lautturi