Which one is more performant?
Stream.of(stringArray).collect(Collectors.toList())
Arrays.asList(stringArray)
Which one is more performant?
Stream.of(stringArray).collect(Collectors.toList())
Arrays.asList(stringArray)
You can do a test by creating a very large array and calculate the elapsed time of the two technics that you mentioned, and then just compare the two elapsed time. here is an example:
String[] veryLargeStringArray = {"","",...};
long starttimelist1 = System.nanoTime();
List<String> list1 = Stream.of(veryLargeStringArray).collect(Collectors.toList());
long endtimelist1 = System.nanoTime();
System.out.println("list1 time : "+(endtimelist1-starttimelist1));
long starttimelist2 = System.nanoTime();
List<String> list2 = Arrays.asList(veryLargeStringArray);
long endtimelist2 = System.nanoTime();
System.out.println("list2 time : "+(endtimelist2-starttimelist2));
And in my opinion, the Stream.of(veryLargeStringArray).collect(Collectors.toList())
is slower one because it has lot od sub-calls than Arrays.asList(veryLargeStringArray)
which does just an instantiation of an ArrayList
. And Finaly it depends on what you want to do in your code.