If I have an expression:
/ List<Long> / / List<String> /
s.getPhones().addAll(Arrays.asList(rs.getString("phones").split(",")));
Can I use Java Lambda to convert types with Long.parseLong in pritty form without "for loop" ? Or even if will use for loop, it will became more "codeless".
How it must look like?
You can convert the List<String>
to a Stream
, map the elements to Long
, then collect them back to a List<Double>
that is suitable to pass to addAll
on s.getPhones()
. No lambda expression is necessary because of the method reference, but streams makes this easier.
s.getPhones().addAll(
Arrays.asList("123,456,789".split(","))
.stream()
.map(Long::valueOf) // You could use: (s -> Long.valueOf(s))
.collect(Collectors.toList()));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.