java 8 - Converting Array to List -


in order convert integer array list of integer have tried following approaches:

  1. initialize list(integer type), iterate on array , insert list

  2. by using java 8 streams:

    int[] ints = {1, 2, 3}; list<integer> list = new arraylist<integer>(); collections.addall(list, arrays.stream(ints).boxed().toarray(integer[]::new)); 

which better in terms of performance?

the second 1 creates new array of integers (first pass), , adds elements of new array list (second pass). less efficient first one, makes single pass , doesn't create unnecessary array of integers.

a better way use streams be

list<integer> list = arrays.stream(ints).boxed().collect(collectors.tolist()); 

which should have same performance first one.

note such small array, there won't significant difference. should try write correct, readable, maintainable code instead of focusing on performance.


Comments