More Than Just List

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.

Comments:

Post a Comment:
Comments are closed for this entry.
About

mduigou

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today