With Java 8, this can be pretty brief:
static <T> List<T> convertToList(
String string, Function<String, T> function) {
return Pattern.compile("\\s").splitAsStream(string)
.map(function).collect(Collectors.toList());
}
public static void main(String[] args) {
String s = "3 4";
System.out.println(convertToList(s,Double::parseDouble));
System.out.println(convertToList(s,Integer::parseInt));
}
, .