Java Reference
In-Depth Information
Java 8 Iterable.foreach
Java 8 adds foreach to the Iterable interface, a default method (discussed in Creating Your
Own Functional Interfaces ) that you don't have to write. Thus, without changing the Ar-
rayIterator , after moving to Java 8 we can use the newest-style loop, Iterat-
or.foreach(Consumer) , with a lambda expression (see Chapter 9 ) to print each element
(see Figure 7-2 ).
Stack
Problem
You need to process data in “last-in, first-out” (LIFO) or “most recently added” order.
Solution
Write your own code for creating a stack; it's easy. Or, use a java.util.Stack .
Discussion
You need to put things into a holding area quickly and retrieve them in last-in, first-out order.
This is a common data structuring operation and is often used to reverse the order of objects.
The basic operations of any stack are push() (add to stack), pop() (remove from stack), and
peek() (examine top element without removing). ToyStack in Example 7-5 is a simple class
for stacking only int s. If you want to stack user-defined objects, see Example 7-6 .
Search WWH ::




Custom Search