Home » Java » String array to a collection of Integer?

String array to a collection of Integer?

Posted by: admin December 28, 2021 Leave a comment


What is an easy way to convert a String[] to a Collection<Integer>? This is how I’m doing it right now but not sure if it’s good:

String[] myStringNumbers;


You don’t need to make an intermediate array. Just parse and collect (with static import of Collectors.toList):



It is unnecessary to use parseInt as it will box the result to the collection, and as @Misha stated you can use Arrays.stream to create the stream. So you can use the following:


Please note that this does not do any error handling (and the numbers should not start with 0, # or 0x in case you do not want surprises). If you want just base 10 numbers, Integer::valueOf is a better choice.


Here is what I think:

String[] myStringNumbers = {"1", "2"};
List<Integer> list = Stream.of(myStringNumbers).map(Integer::valueOf).collect(Collectors.toList());

I hope it can be some help. 🙂