I have a string with comma separated value that I am getting direclty from database. Now I want to pass that entire string to another query but the datatype required is long and i want to use in clause to get this done.
str1 = [123,456,789];
Is there any direct way to do it instead of looping.
A little shorter than virag's answer, as it skips asList() and doesn't do a string trim.
List<Long> list = Arrays.stream(str.split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
You can use the Lambda functions of Java 8 to achieve this without looping
String string = "1, 2, 3, 4";
List<Long> list = Arrays.asList(string.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With