0

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.

Pratik Jaiswal
  • 309
  • 7
  • 26

4 Answers4

5

Probably the simplest way how to create a list from known values is

Arrays.asList(31527)

That would make your call

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

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 the List 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

public static final int[] VALUES = {1, 2, 3, /*...*/};

which you can turn into a List (in Java 8) with Arrays.stream(VALUES).boxed().collect(Collectors.toList()) (see here).

I recommend using Guava library instead where you can create a safer immutable list directly with

public static final List<Integer> VALUES = ImmutableList.of(1, 2, 3) 

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.

Community
  • 1
  • 1
Mifeet
  • 12,949
  • 5
  • 60
  • 108
  • I just edited and extended my current question (avoiding repost and similar question posting). How do I extend to support multiple values as a part of list? Imagine like 1000 different values and writing them directly here won't be the correct way? Rather some file/other constants class? – Pratik Jaiswal May 11 '16 at 22:07
  • I've expanded my answer. You may get a better advice if you explain your use case more, e.g., what are the numbers, where do they come from, etc. – Mifeet May 11 '16 at 22:16
  • If I give array of values in the source code such as data.put("case_ids", Arrays.asList(31527, 31528, 31555));, the API does not throw any error. However, when I create a separate class called Constants.java and store this array like public static final int[] VALUES = {31527, 31528, 31555}; and then call data.put("case_ids", Arrays.asList(VALUES));, I run into error of Invalid JSON Api exception. Is something changing, do I have to cast anything? – Pratik Jaiswal May 11 '16 at 23:35
  • I get this with your updated answer: `method stream(int[]) is undefined for the type Arrays?` Also, does not seem to find `Collectors` - says "Collectors cannot be resolved". Am I missing something? – Pratik Jaiswal May 11 '16 at 23:50
  • You're probably not using Java 8. See the mentioned [question](http://stackoverflow.com/q/754294/2032064) for more options how to convert an array to list. Mostly they recommend using some library, either Guava or Apache. – Mifeet May 12 '16 at 00:06
  • Yeah, not the Java 8 but 7. So the stream() or your solution would not work if I want to use Java 7. – Pratik Jaiswal May 12 '16 at 04:00
1

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

int [] cases = { 10,20,30};
data.put("case_ids", Arrays.asList(cases));
Raghu K Nair
  • 3,854
  • 1
  • 28
  • 45
  • What if I want to pass more than 1 int value like `31527,31528` ...I am trying to read from the constants var like: `public static final int[] CASES = { 22445, 22466, 22467, 27765, 27772 };` and then calling `data.put("case_ids", Arrays.asList(new Integer[] { CASES }))` does not seem to work. See my edited question part. – Pratik Jaiswal May 12 '16 at 00:08
  • it should be Arrays.asList(CASES) thats all – Raghu K Nair May 12 '16 at 04:14
  • I am not sure why its not working . Whats the error you are getting ? which java version you are using. – Raghu K Nair May 12 '16 at 04:24
  • I am not using Java 8 but 7. Here is what I get: Invalid or incomplete "JSON string in API request." – Pratik Jaiswal May 12 '16 at 04:27
  • This is definitely not a Java error . You could execute the following two lines `int [] cases = { 10,20,30}; Arrays.asList(cases)` with out any errors ? we will not be able to help the issues with out seeing the whole code. – Raghu K Nair May 12 '16 at 04:34
  • You are right, not the Java error but something with my 3rd party API. I will get back once I have more information. – Pratik Jaiswal May 12 '16 at 04:44
0
List<Integer> mList = new ArrayList<Integer>;
mList.add(31527);
data.put("case_ids", mList);
Oussema Aroua
  • 5,225
  • 1
  • 24
  • 44
  • 2
    *"Any answer that gets the asker going in the right direction is helpful, but do try to mention any limitations, assumptions or simplifications in your answer. **Brevity is acceptable, but fuller explanations are better**."* - [How do I write a good answer?](http://stackoverflow.com/help/how-to-answer) – Jonny Henly May 11 '16 at 21:43
  • Thanks Oussema, I just edited my question and want to have a support for multiple values in the list. – Pratik Jaiswal May 11 '16 at 22:10
  • the List is an Collection so you can store as many value as you want at one list without caring about size. – Oussema Aroua May 11 '16 at 22:14
0

List is an interface. Use a concrete class like data.put("case_ids", new ArraList<Integer>{ 31527 });