Which one is more efficient in using a list of arrays?

Which one is more efficient at instantiating a list?

List<Type> list = new ArrayList<Type>(2); list.add(new Type("one")); list.add(new Type("two")); 

OR

 List<Type> list = Arrays.asList(new Type("one"), new Type("two")); 
+6
source share
1 answer

They create different types of objects. new ArrayList<>() creates a java.util.ArrayList that you can add, etc.

Arrays.asList() uses a type that is also called ArrayList , but is a nested type ( java.util.Arrays$ArrayList ) and does not allow you to add or remove elements. It just wraps the array.

Now, if you don't care about these differences, you will get two roughly equivalent implementations, both wrapping the array in the List<> interface. I would be very surprised if they differed in performance in any significant way - but, as before, if you have certain performance problems, you should check them in your specific context.

+14
source

All Articles