SinglyLinkedList implements PStack<E> { @Override public void forEach(Consumer<E> action) { SinglyLinkedList<E> c = this; while (c != null) { action.accept(c.element); c = c.next; } } private  SinglyLinkedList(E element); private  SinglyLinkedList(E element, SinglyLinkedList<E> next); @Override PStack<E> push(E e); @Override E peek(); @Override E peek(int i); @Override PStack<E> pop(); @Override boolean isEmpty(); @Override void forEach(Consumer<E> action); @Override boolean anyMatch(Predicate<E> predicate); @Override int size(); @Override int hashCode(); @Override String toString(); @Override boolean equals(Object obj);  }
@Test void forEach() { List<Object> consumer = new ArrayList<>(); PCollections.emptyStack().forEach(consumer::add); assertThat(consumer).isEmpty(); Object a = new Object(); Object b = new Object(); PStack<Object> s = PCollections.emptyStack().push(b).push(a); s.forEach(consumer::add); assertThat(consumer).isEqualTo(ImmutableList.of(a, b)); }