Idea 1
T feed(T item) returns the element T of the reservoir that was replaced by item or null if the sample was not modified as a result of this operation.
Idea 2
A new feed method has to return values from 3 different cases:
item was not selected and there was no change in the sample (return null)
item was inserted in the sample but nothing was removed (return null?)
item replaced an existing item T of the sample (return T)
An obvious solution is to return Optional<T>:
Optional<T> feed(T item) returns null in case (1), an Optional<T> that doesn't hold a value in case (2) and an Optional<T> that holds the reference to the item that was removed in case (3).
Idea 1
T feed(T item)returns the elementTof the reservoir that was replaced byitemornullif the sample was not modified as a result of this operation.Idea 2
A new
feedmethod has to return values from 3 different cases:itemwas not selected and there was no change in the sample (returnnull)itemwas inserted in the sample but nothing was removed (returnnull?)itemreplaced an existing itemTof the sample (returnT)An obvious solution is to return
Optional<T>:Optional<T> feed(T item)returnsnullin case (1), anOptional<T>that doesn't hold a value in case (2) and anOptional<T>that holds the reference to the item that was removed in case (3).