views:

76

answers:

2

Today I was reading through some Hibernate code and I encounter something interesting. There is a class called CollectionHelper that defines the following constant varibale:

public final class CollectionHelper {

   public static final List EMPTY_LIST = Collections.unmodifiableList( new ArrayList(0 ) ;
public static final Collection EMPTY_COLLECTION = Collections.unmodifiableCollection(new ArrayList(0) );
public static final Map EMPTY_MAP = Collections.unmodifiableMap( new HashMap(0) );

They have used these constants to initialize collections with immutable instances. Why they didn't simply use the Collections.EMPTY_LIST for initializing lists? Is there a benefit in using the following method?

+1  A: 

No, there is no benefit. The only difference apparent to me is that this method is guaranteed to return a different EMPTY_LIST than any List created with Collections.emptyList(), whereas implemenations of Collections.emptyList() may or may not return the same List instances. I am more inclined to agree with @WizardOfOdds's comment that they simply didn't know about those API functions.

danben
A: 

The may be times when it improves performance by reducing the number of implementations used at a particular call-site, allowing better monomorphic and bimorphic inlining optimisations. Bit of a long shot, though.

Tom Hawtin - tackline