For removing duplication java people would have added an extra class in list itself then what is the use of creating an entire hierarchy ??
2 Answers
A Set cannot contain duplicate elements while a List can. A List (in Java) also implies order. Conceptually we usually refer to an unordered grouping that allows duplicates as a Bag and doesn't allow duplicates is a Set. List is used to collection of elements with duplicates.
For more, you can go through this link: What is the difference between Set and List?
When to use List, Set and Map in Java?
1) If you do not want to have duplicate values in the database then Set should be your first choice as all of its classes do not allow duplicates.
2) If there is a need of frequent search operations based on the index values then List (ArrayList) is a better choice.
3) If there is a need of maintaining the insertion order then also the List is a preferred collection interface.
4) If the requirement is to have the key & value mappings in the database then Map is your best bet.
Efficiency, mostly.
Sets are implemented in a way that makes lookup efficient. If you want to lookup whether an element exists in a set, it only takes logarithmic time. If you want to lookup for whether a list contains an element, you need to (pessimistically) traverse the whole list.
Imagine you have a dictionary of 200k words and want to check a text containing of 50k words for whether they are in the dictionary. Doing that with a list dictionary required roughly 100k (half of the list on average) * 50k operations. Doing that on a set only requires log(200k) * 50k, where log(200k) ~= 16.

- 3,446
- 10
- 18
-
Thanks for feed back Then we can eliminate list instead of set – raghu ram Jul 28 '16 at 18:00
-
1But on the other hand, set doesn't keep ordering, so if you need to keep things in order, having a set doesn't work. – Piotr Wilkin Jul 28 '16 at 18:01