Single Item List Showdown: Collections::singletonList vs. List::of
How do you take a single Java object of type
T and turn it into a single-element
One way, of course, is to instantiate some List implementation like
LinkedList and add the item, but where's the fun in that? Saavy developers like us want to do such banal things in a single line of code. The good news is that JavaSE provides multiple single-line-of-code approaches to address this problem.
(I'm going to ignore the so-called "double brace" instantiation approach because even though you can create the single-item list and assign a reference in one statement, it uses two lines of code: one line to instantiate the anonymous List subtype and one line inside the initializer block to add the item.)