135 Java - Collection Factory Methods
Factory Methods for Collection In Java 9, collections were enhanced to have few new methods to create immutable list in an easy and concise way. These new factory methods were added to List, Set, and Map interfaces to create immutable instances. These factory methods are mainly convenience factory methods in order to create a collection in less verbose and in concise way. Syntax Before Java 9, following syntax was used to create a immutable list. List unmodifiableList = Collections . unmodifiableList ( arrayList ) ; Where arrayList is a mutable list instance. So we are required to create a list and then using unmodifiableList() we get an immutable instance from which we cannot add/remove an element. Factory Methods of List Interface Now from Java 9, following methods can be used to create a immutable list. static < E > List < E > of ( ) ; static < E > List < E > of ( E e1 ) ; static < E > L...