Here you can find the source of peek(Stack
public static <V> Object peek(Stack<Object> stack, Class<?> type)
//package com.java2s; //License from project: LGPL import java.util.Stack; public class Main { public static <V> Object peek(Stack<Object> stack, Class<?> type) { Object result = null;/* w ww .ja va 2s. com*/ Object value = null; while (!((value = stack.peek()).getClass().isAssignableFrom(type))) { stack.pop(); } if (value != null && value.getClass().isAssignableFrom(type)) result = value; return result; } public static <V> Object pop(Stack<Object> stack, Class<?> type) { Object result = null; Object value = null; while (!((value = stack.pop()).getClass().isAssignableFrom(type))) { ; } if (value != null && value.getClass().isAssignableFrom(type)) result = value; return result; } }