Walking methods

The StackWalker defines the forEach() method that expects a Consumer (preferably in the form of a lambda expression) that is invoked for each element of the stack trace walking up the stack. The argument to the Consumer method is a StackFrame object.

Although a method named forEach is also defined by the Stream interface and the method walk() passes a Stream object to the Function it gets as argument, we should not confuse the two. The forEach() method of StackWalker is a simpler, and most of the time less effective way to get through all the elements of the stack trace.

It is less effective, in most cases, because it forces the StackWalker instance to get all the elements of the stack trace so that the forEach() method can ...

Get Java 9: Building Robust Modular Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.