Generalizing solution to collections instead lists