1

I have a String array and I want to convert it into BigInteger array using Java 8 streams.

String[] output = bigSorting(new String[]{"31415926535897932384626433832795", "1", "4900146572543628830293235422623540449026979", "10", "57500297590012603652986133599394871645776460", "5", 
                    "497010206818067722087306230802257700034825862515267073569769100385728461314", "57500297590012603652986133599394871645776460497010206818067722087306230802257700034825862515267073569769100385728461314497010206818067722087306230802257700034825862515267073569769100385728461314497010206818067722087306230802257700034825862515267073569769100385728461314497010206818067722087306230802257700034825862515267073569769100385728461314497010206818067722087306230802257700034825862515267073569769100385728461314"});
        Object[] unsortedBigIntegerArr = convertFromStringArrayToBigIntegerArray(output);

This is what I have tried but I am not able to get BigInteger array, I am able to get the Object array though.

private static Object[] convertFromStringArrayToBigIntegerArray(String[] unsorted) {
        return Arrays.stream(unsorted).map(BigSorting2::convertFromStringToBigInteger).toArray();
    }

    private static BigInteger convertFromStringToBigInteger(String unsorted) {
        return new BigInteger(unsorted);
    }

Is there any way I can do it completely using Java 8 streams.

0

2 Answers 2

5

As simple as:

return Arrays.stream(unsorted)
             .map(BigSorting2::convertFromStringToBigInteger)
             .toArray(BigInteger[]::new);
Sign up to request clarification or add additional context in comments.

10 Comments

Holy cow, this is wicked cool, thank you. Can you please suggest me best learning paths for Java8 streams.
@pjj I'd start with Oracle tutorials really and the official documentation...
you could even do .map(BigInteger::new)
@pjj I learned quite a lot from Stack Overflow questions and answers. Search for the [java-stream] tag, for example.
@pjj to be honest most people unfamiliar with the Java type system would expect your attempt of Arrays.stream(unsorted).map(BigSorting2::convertFromStringToBigInteger).toArray(); to yield a Stream<BigInteger> but obviously this is not possible. due to a limitation of java one would have to explicitly specify an array constructor reference to retrieve an array of the expected type hence the .toArray(BigInteger[]::new); in the answer.
|
0

Something really simple like:

public void test(String[] args) {
    String[] output = new String[]{"31415926535897932384626433832795", "1", "4900146572543628830293235422623540449026979", "10", "57500297590012603652986133599394871645776460", "5",
            "497010206818067722087306230802257700034825862515267073569769100385728461314", "57500297590012603652986133599394871645776460497010206818067722087306230802257700034825862515267073569769100385728461314497010206818067722087306230802257700034825862515267073569769100385728461314497010206818067722087306230802257700034825862515267073569769100385728461314497010206818067722087306230802257700034825862515267073569769100385728461314497010206818067722087306230802257700034825862515267073569769100385728461314"};
    Object[] bigIntegers = Arrays.stream(output)
            .map(BigInteger::new)
            .toArray();
    System.out.println(Arrays.toString(bigIntegers));
}

2 Comments

I think you mis-understood my question, please see my question or @Eugene answer, thanks for your answer though.
As I understood the question it was whether it was possible to get a BigInteger[] rather than an Object[] from the String[].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.