The best way to assign an immutable instance to a Collection in Java

Posted by Ali on Stack Overflow See other posts from Stack Overflow or by Ali
Published on 2010-05-02T15:05:56Z Indexed on 2010/05/02 15:07 UTC
Read the original article Hit count: 289

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about collections