Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1092431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:40:53+00:00 2026-05-16T23:40:53+00:00

Is there an elegant way to turn an array of primitives into an array

  • 0

Is there an elegant way to turn an array of primitives into an array of the corresponding container objects — turn a byte[] into a Byte[], for example? Or am I stuck with looping through it and doing it manually?

Yeah, the for loop isn’t exactly difficult. Just kinda ugly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:40:54+00:00Added an answer on May 16, 2026 at 11:40 pm

    Apache Commons

    Apache Commons / Lang has a class ArrayUtils that defines these methods.

    • All methods called toObject(...)
      convert from primitive array to wrapper array
    • All called toPrimitive(...) convert
      from wrapper object array to
      primitive array

    Example:

    final int[]     original        = new int[] { 1, 2, 3 };
    final Integer[] wrappers        = ArrayUtils.toObject(original);
    final int[]     primitivesAgain = ArrayUtils.toPrimitive(wrappers);
    assert Arrays.equals(original, primitivesAgain);
    

    Guava

    But then I’d say that Arrays of wrapped primitives are not very useful, so you might want to have a look at Guava instead, which provides Lists of all numeric types, backed by primitive arrays:

    List<Integer> intList = Ints.asList(1,2,3,4,5);
    List<Long> longList   = Longs.asList(1L,2L,3L,4L,5L);
    // etc.
    

    The nice think about these array-backed collections is that

    1. they are live views (i.e. updates to the array change the list and vice-versa)
    2. the wrapper objects are only created when needed (e.g. when iterating the List)

    See: Guava Explained / Primitives


    Java 8

    On the other hand, with Java 8 lambdas / streams, you can make these conversions pretty simple without using external libraries:

    int[] primitiveInts = {1, 2, 3};
    Integer[] wrappedInts = Arrays.stream(primitiveInts)
                                  .boxed()
                                  .toArray(Integer[]::new);
    int[] unwrappedInts = Arrays.stream(wrappedInts)
                                 .mapToInt(Integer::intValue)
                                 .toArray();
    assertArrayEquals(primitiveInts, unwrappedInts);
    
    double[] primitiveDoubles = {1.1d, 2.2d, 3.3d};
    Double[] wrappedDoubles = Arrays.stream(primitiveDoubles)
                                    .boxed()
                                    .toArray(Double[]::new);
    double[] unwrappedDoubles = Arrays.stream(wrappedDoubles)
                                      .mapToDouble(Double::doubleValue)
                                      .toArray();
    
    assertArrayEquals(primitiveDoubles, unwrappedDoubles, 0.0001d);
    

    Note that the Java 8 version works for int, long and double, but not for byte, as Arrays.stream() only has overloads for int[], long[], double[] or a generic object T[].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an elegant way to have performant, natural sorting in a MySQL database?
Is there some elegant way to add an empty option to a DropDownList bound
Is there an elegant way to specialize a template based on one of its
Is there an elegant way to create and initialize a const std::vector<const T> like
Is there an elegant way in Perl to find the newest file in a
Is there an elegant way to handle exceptions that are thrown in finally block?
Is there any elegant way of applying a certain style to all <input type=text>
In Java, is there an elegant way to detect if an exception occurred prior
Would there a more elegant way of writing the following syntax? Thread t0 =
I was just wondering if there is an elegant way to set the maximum

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.