Due by the end of this meeting
interface
named SingleAccess.
It represents an abstraction
over the kinds of containers we discussed last week:
Stack, Queue and PriorityQueue
(as well as the Hat which was mentioned briefly).
Your interface includes the following methods:
void add(E)
E remove()
E element()
boolean offer(E)
E poll()
E peek()
boolean isEmpty()
int size()
boolean addAll(Container<? extends E> action)
void clear()
void forEach(Consumer<? super E> action)
All but the last of these behave exactly as the same-named Queue operations do (some of which are inherited from Collection).
The last is similar to the forEachRemaining method
from the Iterator interface.
Here is a suitable javadoc for our version:
Provide default definitions for the last three methods listed above.
For your reference, here is the code for Iterator'sforEachRemainingmethod, whose behaviour is pretty much exactly what we want:default void forEachRemaining(Consumer<? super E> action) { Objects.requireNonNull(action); while (hasNext()) action.accept(next()); }
remove and poll methods
return a randomly-selected object.
The methods element and peek
throw UnsupportedOperationExceptions
with the message Not supported.
Make the methods as efficient as you can. Use an appropriate Java Collection to hold the elements.
addAll to add the names
Mal, Zoe, Kaylee, Wash, Jayne, Book, Simon, River, Jayne
(NOTE: Jayne is there twice)
to a Hat,
and then uses the forEach method
to remove and print each one.
(The names should come out in a random order.)
Submit this/these files: