What is the difference between a Set and List?
A very common question asked in Java interviews is to explain the difference between a Set and a List . So,in this post I’m going to explain this in detail. What is common between Set and List? Before I start with the differences, let me explain what is common between the two. Both Set and List are sub interfaces of the Collection interface in the java.util package. Both store a group of elements, however they differ slightly in the way they store data. So what are the differences? Duplicates A List allows duplicate elements i.e. the same element can be present more than once. A Set on the other hand does not allow duplicates. So if the same element is added again, it will overwrite the previous value of the element. Ordering of Elements Also a List is ordered i.e. elements in a list are stored in the order in which they are inserted & this order is maintained. The List interface has a method called add. It adds object sequentially to the list, so the first object added will