0

I have a Java map:

Map<String, Object> data = new HashMap<String, Object>();

And I am doing following put operation on it by:

data.put("case_ids", Arrays.asList(22222));

where case_ids is the attribute for 3rd party API and it accept int values. Here when I input multiple int values in Arrays.asList(), the Api request works fine. For example, Arrays.asList(22222, 1111) works with no issues. However, there is a case where I will have different 100 int values and it will not be a good way to have all the values directly in the code/method. So, I am thinking of creating a constant.java class and have

public static final int[] CASES = { 
        11111, 22222, 33333, 44444, 55555,....
    };

in the constants java class. However when I make a call to

data.put("case_ids", Arrays.asList(Constants.CASES));, I get the API exception of Invalid JSON request.

I do not use Java 8 and so, stream() function would not work for me. I tried this Arrays.stream(VALUES).boxed().collect(Collectors.toList()) as per Java List Object instead of an Array.

Community
  • 1
  • 1
Pratik Jaiswal
  • 309
  • 7
  • 26
  • Then, see [here](http://stackoverflow.com/questions/754294/convert-an-array-of-primitive-longs-into-a-list-of-longs). – Sotirios Delimanolis May 12 '16 at 04:23
  • `asList` is defined as ` List asList(T... a)`. Since `T` cannot be a primitive, the compiler infers that `T` is an `Integer`, making it `List asList(Integer... a)`, which then means that `22222` and `1111` gets autoboxed to `Integer`. If you change your constant to `Integer[] CASES`, then it will work. – Andreas May 12 '16 at 04:28

0 Answers0