1

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?.

1

3 Answers 3

0

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.

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

4 Comments

bro but why are you contradicting yourself by saying "there is no significant difference between ArrayList and JSONArray" and also saying "the most efficient way would be to use ArrayDeque with JSONObject instead of JSONArrays" I am kinda confused now
I said use ArrayDeque with JSONObjects because it is the fastest collection. There is no difference if you use ArrayList vs JSONArray.
Is there any source which describes ArrayDeque is faster than arraylist or any logic behind that please explain or give me some source. I have asked this question since most people have said this is better, and that is better but none gave right explanations.
See this link for benchmarks
0

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

Please add a source that says ArrayNode is faster thanks!
You will be able to feel performance difference if you use values that are overloaded for ArrayNode#add(...) method. In this case there will be no class casting which typically takes a lot of time. JSONObject accepts all types as Object and when you will use it there will be a lot of class casting performed. But if you need to store just some POJOs I believe there will be no so perceptible difference in performance. The only one thing that ArrayNode will provide you much more features than other ways.
0

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.

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.