Package generic.util
Class DequePush<E>
java.lang.Object
generic.util.DequePush<E>
- Type Parameters:
E- the type of element pushed to the stack
- All Implemented Interfaces:
AutoCloseable
A context utility allowing stack management via a try-with-resources block
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
-
Field Details
-
stack
-
-
Constructor Details
-
DequePush
-
-
Method Details
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
push
Push an element to the given stack- Parameters:
stack- the stackelem- the element- Returns:
- a context used to pop the element
This is an idiomatic convenience, as in a try-with-resources block:
This idiom can be very useful if there is complex logic between the push and pop. It's easy to forget to pop; however, this convenience comes at the cost of a heap allocation.Deque<String> stack = new LinkedList<>(); try(DequePush<?> p = DequePush.push(stack, "Hello, World!\n")) { System.out.println(stack.peek()); }
-