More Than Just List
By mduigou on Jul 30, 2010
I've noticed that a lot of people who use the java.util Collections classes in their APIs will most often use List. Sometimes to the exclusion of the other Collections types. The other core Collections classes Set, Map and Collection are under-represented in most public Java APIs.
In the APIs I've written, especially since the introduction of generics, I try to use the other types more liberally. Why not just use List? To me List implies "ordered" and "duplicates allowed". If ordering isn't a relevant characteristic for the values I'll use Collection. If I wish to indicate that duplicate elements are not allowed I'll use Set or for cases when order does matter, SortedSet. When the collection
Using the right Collection type provides the API user a good hint as to key characteristics of the collection data. Using only List in APIs makes those characteristics less obvious and may lead to mis-use or abuse of APIs.