Reputation: 351
In some code from my company, I've read a list initialization:
List<SomeClass> someClassList = new ArrayList<>(Collections.emptyList())
What does it mean? A list with 0 size initialization?
Upvotes: 1
Views: 192
Reputation: 339797
That code with Collections.emptyList()
:
new ArrayList<>(Collections.emptyList())
… is either a misguided micro-optimization (see below) or silly nonsense.
Collection
to the constructor results in the elements being assigned to the new list.Collection
has no effect. The result is a new empty list, just as if you had used the no-arg constructor.So deliberately passing a hard-coded empty collection makes no sense practically speaking.
All three of the following examples result in an empty list, an instantiated ArrayList
containing zero elements.
List < Person > persons = new ArrayList<>( Collections.emptyList() ) ; // Empty list.
… and:
List < Person > persons = new ArrayList<>( List.of () ) ; // Empty list.
… and:
List < Person > persons = new ArrayList<>() ; // Empty list.
Only the last one is wise. The other two distract and confuse the reader, as evidenced by the posting of this Question.
There is one small technical difference between the first two examples above and the third. They may give the new ArrayList
a different initial capacity.
(See Comments below for discussion of these implementation details.)
So passing an empty collection might save some octets of memory (or might not). But with computers typically sporting dozens to hundreds of billions of octets, there is no practical point to such a micro-optimization in most situations. Furthermore, those saved octets will be spent the moment your app adds an element object reference to the list. And, even if this micro-optimization works in one version of Java, it may not work in another version when such implementation details may change.
Some programmers coming from programming languages requiring manual memory management (like C) may feel the need to take more control than is necessary when working in an environment like Java with sophisticated managed-memory features. The modern implementations of Java are highly tuned and performant. Such optimization attempts as seen in this Question are frequently premature (no proven need), ineffectual, or even counter-productive.
Generally it’s best in Java to write simple straight-forward code that is easy to read and comprehend. Then let the JVM do its thing.
ArrayList#trimToSize
The only optimization I routinely consider with ArrayList
is calling trimToSize
when done adding/removing elements. And even then, I would only bother to do so on very large lists that may have grown to have a large number of extra slots. Or I might pass the modifiable list to List.copyOf
to produce an unmodifiable list with presumably few to no extra slots.
Upvotes: 12
Reputation: 14415
Yes, it initializes an ArrayList
with a size of "0" but in a rather obscure way, given that there is an appropriate constructor for that:
List<SomeClass> someClassList = new ArrayList<>(0);
By default, new ArrayList
instances have a capacity of 10, so I would argue that it is also not obvious why that size of 0 is required. Unless reasoning given by comments or context, I would probably simply replace it with:
List<SomeClass> someClassList = new ArrayList<>();
Upvotes: 4
Reputation: 8674
Collections.emptyList()
returns an immutable list (i.e. a list where you cannot add elements).new ArrayList(Collection<? extends E> c)
creates a new ArrayList
with the elements in the specified collection.So List<SomeClass> someClassList = new ArrayList<>(Collections.emptyList())
is one way to initilize a mutable ArrayList
with no elements
Upvotes: 2