1

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?

1 Answer 1

2

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()));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.