Wednesday, February 24, 2010

Semantics of Collections

What is up with the java.util.List?
I see it so frequently used, and most of the times to return collection of elements that are not ordered.

The true semantic of the List is  an ordered Collection
Most of the case the API returns unordered elements that are not duplicated. It is then the true semantic of a  java.util.Set.

If the API returns an unordered, collection of possibly duplicated elements, then the returned type should be a Bag (or Multiset from Google Collections) , and the semantic would be correct.

At least, in doubt I'd rather get a java.util.Collection than the wrong returned type.

No comments: