How do I get only the first two stackFrames from StackFrameStream?… here is a solution to the problem.
How do I get only the first two stackFrames from StackFrameStream?
I got a list of StackFrames
by using the walk
method on StackFrameStream
.
But I just need to find the first 3 stackFrames.
I have StackFrameStream
List<StackFrame> stackFrameList =
StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).walk(stackFrameStream -> stackFrameStream.collect(Collectors.toList()));
System.out.println("All frames : \n" + stackFrameList.toString());
for (StackFrame stackFrame : stackFrameList) {
System.out.println("stackFrame.getDeclaringClass()=>" + stackFrame.getDeclaringClass().toString());
System.out.println("stackFrame.getLineNumber=>" + stackFrame.getLineNumber());
System.out.println("stackFrame.getMethodName=>" + stackFrame.getMethodName());
System.out.println();
}
I don’t want to use stackFrameStream.collect(Collectors.toList())
and get the entire list of stackFrames
I only want the first 3 elements
Solution
Use limit
truncatencing:
StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)
.walk(stackFrameStream -> stackFrameStream.limit(3).collect(Collectors.toList()));