I am having List values as ["12-dept20","13-dept50"] i want to split these to a list with only the numbers before - , to get a List as [12, 13]
List. split using streams , but not sure on how to work on it. Any help would be appreciated. I am using java 8
List src = ["12-dept20","13-dept50"];
Expected:
List new = [12, 13]
Here you go:
List<String> source = List.of("12-dept20", "13-dept50");
List<Integer> target = source.stream()
.map(entry -> entry.split("-")[0])
.map(Integer::valueOf)
.toList();
System.out.println(target);
Bear in mind that this code is not failsafe, in case the entries cannot be parsed due to missformat.