I need to convert comma separated string to list of integers. For example if I have following string
String numbersArray = \"1, 2, 3, 5, 7, 9,\";
If you're not on java8, then you can use Guava
Lists.transform(Arrays.asList(numbersArray.split(",")), new Function<String, Integer>() {
@Override
public Integer apply(String input) {
return Integer.parseInt(input.trim());
}
});
As @Maroun has mentioned for Java8 you can use Streams.
int[] numbers = Arrays.asList(numbersArray.split(","))
.stream()
.map(String::trim)
.mapToInt(Integer::parseInt).toArray();