I am refactoring my old code which involves large amount of data parsing and manipulation of JSONObjects inside JSONArrays. I want to know whether it would be efficient to store JSONObjects in ArrayLists. I also want suggestions on What Collection should i use for most performance on Update and Insert operations?.
3 Answers
JSONArray internally uses an ArrayList so it's just like wrapper over ArrayList. I would say that there is no significant difference between ArrayList and JSONArray. JSONObject uses HashMap internally.
As for Collections, ArrayDeque is the fastest, by which the most efficient way would be to use ArrayDeque with JSONObject instead of JSONArrays.
4 Comments
I think the best way is to use jackson ArrayNode and other usefull things from there:
Maven Dependency
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
Code to Start With:
ArrayNode jsonArray = new ObjectMapper().createArrayNode();
ArrayNode jsonArray = new ObjectMapper().createArrayNode();
jsonArray.addPOJO(myObject);
jsonArray.add(232);
jsonArray.add("Hi Stack");
jsonArray.add(anotherJsonArray);
Here is the link to the doc: http://fasterxml.github.io/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/node/ArrayNode.html
2 Comments
ArrayList is faster.
JSONArray uses ArrayList. So if you can do what you need in ArrayList, that will prevent additional references...at the very least. . So if you can do what you need in ArrayList, it's a better choice. If you need the features of JSONArray though it's doubtful that your code will be faster.