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 8402519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:05:32+00:00 2026-06-09T22:05:32+00:00

I have an array of int: int[] a = {1, 2, 3}; I need

  • 0

I have an array of int:

int[] a = {1, 2, 3};

I need a typed set from it:

Set<Integer> s;

If I do the following:

s = new HashSet(Arrays.asList(a));

it, of course, thinks I mean:

List<int[]>

whereas I meant:

List<Integer>

This is because int is a primitive. If I had used String, all would work:

Set<String> s = new HashSet<String>(
    Arrays.asList(new String[] { "1", "2", "3" }));

How to easily, correctly and succinctly go from:

A) int[] a...

to

B) Integer[] a ...

Thanks!

  • 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-06-09T22:05:34+00:00Added an answer on June 9, 2026 at 10:05 pm

    Some further explanation. The asList method has this signature

    public static <T> List<T> asList(T... a)
    

    So if you do this:

    List<Integer> list = Arrays.asList(1, 2, 3, 4)
    

    or this:

    List<Integer> list = Arrays.asList(new Integer[] { 1, 2, 3, 4 })
    

    In these cases, I believe java is able to infer that you want a List back, so it fills in the type parameter, which means it expects Integer parameters to the method call. Since it’s able to autobox the values from int to Integer, it’s fine.

    However, this will not work

    List<Integer> list = Arrays.asList(new int[] { 1, 2, 3, 4} )
    

    because primitive to wrapper coercion (ie. int[] to Integer[]) is not built into the language (not sure why they didn’t do this, but they didn’t).

    As a result, each primitive type would have to be handled as it’s own overloaded method, which is what the commons package does. ie.

    public static List<Integer> asList(int i...);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem i need to convert from my Array structure to std::vector<int> ...
I have an int[] array. I need to take an int and append it
I have an array: int[][] lawn = new int[980][1280]; wich stores the values of
i have an array like below int[] array = new array[n];// n may be
I have an int array containing gray scale values from 0-254, i also have
I need to pass an array of objects, which contain arrays, from javascript to
I have an Array like below in PHP array( (int) 0 => array( 'id'
Let us say I have an array int aVar[10]; ... ... for(i=0; i<10; i++)
i have two int array of images , i have implemented it in grid
I have an int array which has no elements and I'm trying to check

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.