If you are already using Guava, you can do this using the following one-liner:
List<String> l = FluentIterable.from(l1).append(l2).copyInto(new ArrayList<String>());
I personally don't like double braces. This produces new class just for the purpose to initialize it in different way. New class means bigger target jar file, more memory is necessary for JVM to store classes, bigger inheritance table for JIT compiler, etc. This can become significant if you use such approach everywhere. If you return this ArrayList
to another component and your current component is not used anymore (for example, you are using OSGi or other module system), you may end up having hanging ClassLoader
which is linked only by this anonymous class. Also if you care about serialization, you should declare serialVersionID
in this new class. Even if you don't care, you may have a warning about this.