0

I need a common static function for convert comma separated string to a generic list.

String str = "1,2,4,5";
List<BigInteger> lists = Stream.of(str.split(",")).map(String::trim).map(BigInteger::new).collect(Collectors.toList());

Instead of this .map(BigInteger::new) I need a generic expression to convert to generic list

1
  • 2
    Sounds like you need a Function<String, T> to convert a string to your generic type. Commented Sep 22, 2020 at 8:56

1 Answer 1

4

The BigInteger::new step executes a Function<String, BigInteger> to convert each string to an instance of BigInteger. If you want to do this for a generic type, you need a function to convert a string to an instance of your generic type. That means you need a Function<String, T>.

Given Function<String, T> converter, you can do:

List<T> items = Stream.of(str.split(","))
                      .map(String::trim)
                      .map(converter)
                      .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.