0

I'm trying to find best way to convert List of Integers to JSONArray of org.json.JSONArray library for API call. For that purpose I'm trying to implement stream() methods of Java 8.

Given

List<Integer> ids = new ArrayList<>();

ids.add(1);

ids.add(2);

ids.add(3);

Needed JSONArray object

JSONArray ids = [1,2,3]

with using stream() method in Java.

1
  • 2
    Why do you need to use the stream method? Commented May 6, 2022 at 15:09

3 Answers 3

1

You can do it easily without using streams by using the constructor of the JSONArray class which accepts a Collection:

List<Integer> ids = List.of(1, 2, 3);
JSONArray json = new JSONArray(ids);

If you have to use streams for some reason:

List<Integer> ids = List.of(1, 2, 3);
JSONArray json = new JSONArray();
ids.stream().forEach(json::put);

As pointed out by Alexander Ivanchenko in the comments however, you should avoid doing this.

Sign up to request clarification or add additional context in comments.

4 Comments

You don't need to create a stream, instead you can invoke forEach() on a list. It will be both cleaner and cheaper.
I am aware, but the poster also specifically asked for an implementation using a stream which is why I added an alternative including it.
But the fact that the way in which you are advising to use streams in not encouraged by the API documentation still remains. Take a look at the code examples in the part dedicated to side-effects.
I was not trying to advise to use them but only wanted to provide a solution that fits the posters requirement of using a stream, sorry if this was unclear. I have added a note regarding what you pointed out.
0

You can use mutable reduction with collect().

Note, that usage of streams is justifiable only if you need to filter or change some values along the way, otherwise it'll be an unnecessary complication of the code.

List<String> source = List.of("1", "2", "3");

JSONArray result =
    source.stream()
          .map(str -> str.concat(".0"))
          .collect(
              JSONArray::new,
              JSONArray::put,
              JSONArray::putAll);
    
System.out.println(result);

Output

["1.0","2.0","3.0"]

Comments

0

Naive solution: A naive solution is to iterate over the given set and copy each encountered element in the Integer array one by one.

Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5));
Integer[] array = new Integer[set.size()];
 
int k = 0;
for (Integer i: set) {
    array[k++] = i;
}
 
System.out.println(Arrays.toString(array));

Using Set.toArray() method: Set interface provides the toArray() method that returns an Object array containing the elements of the set.

Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5));
Object[] array = set.toArray();
System.out.println(Arrays.toString(array));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.