Just before you ditch bags forever, there is a particular case in which bags (and also lists) are much more performant than sets. For a collection with inverse="true"
(the standard bidirectional one-to-many relationship idiom, for example) we can add elements to a bag or list without needing to initialize (fetch) the bag elements! This is because Collection.add()
or Collection.addAll()
must always return true for a bag or List
(unlike a Set
). This can make the following common code much faster.