p hasini wrote:2. How can we avoid duplicates in a list
you cant . if you want to avoid duplicates *use Set*
Well you can, but you'll need to call contains first:
But you can't subclass a List implementing class and add override the add methods to add this check since it breaks the contract for List -- it explicitly allows duplicates. A LinkedHashSet is a good alternative for a collection without duplicates that maintains insertion order.