I hava a Java map like this:
Map<String, Object> data = new HashMap<String, Object>();
And I am performing following operation on it:
data.put("case_ids", new int[] { 31527 });
Where case_ids is one of the attribute of 3rd party API. However, that API throws incorrect API request error
and suggests me to use List object instead of an Array for:
data.put("case_ids", new int[] { 31527 });
Not an expert of List, any suggestions? Like data.put("case_ids", new List[] { 31527 });
?
EDITED: Extending my current question (to avoid repost or similar question). What if I have huge set of values for above put operation and want to create a list of known values. I do not feel that it is a correct way to mention the list of values separated by commas like 31527, 31528, 31529, 31530, 31531,..etc. I would rather store this somewhere and just call that as a input here - data.put("case_ids", new int[] { input });
. Also, is that possible to do without a file? Just want to avoid dependency on file.
List is an interface. Use a concrete class like
data.put("case_ids", new ArraList<Integer>{ 31527 });
Probably the simplest way how to create a list from known values is
That would make your call
supposing the API accepts a
List
as the value. Without the API documentation or the error message, we cannot tell what it really accepts.There are other ways how to create List, such as
new ArrayList<>()
, etc., just check out the javadoc and concrete classes which implement theList
interface.Update: If you don't want an external file, I suppose you're fine with having the numbers in the source code. The simplest option is to have them somewhere as a static field
which you can turn into a
List
(in Java 8) withArrays.stream(VALUES).boxed().collect(Collectors.toList())
(see here).I recommend using Guava library instead where you can create a safer immutable list directly with
If you don't like having all values in the code like this, then you can read them from a resource file, but that would be for another question.
You could use the following format
data.put("case_ids", Arrays.asList(new Integer[] { 31527 }))
;If you have an array of integers the one you have mentioned the comments then you can use