Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java HashMap vs JSONObject

Tags:

java

json

hashmap

I am wondering about the performance of Java HashMap vs JSONObject.

It seems JSONObject stores data internally using HashMap. But JSONObject might have additional overhead compared to HashMap.

Does any one know about the performance of Java JSONObject compared to HashMap?

Thanks!

like image 224
user414585 Avatar asked Sep 08 '10 17:09

user414585


People also ask

What is the difference between MAP and JSONObject?

A JSONObject is an unordered collection of name/value pairs whereas Map is an object that maps keys to values. A Map cannot contain duplicate keys and each key can map to at most one value. We need to use the JSON-lib library for serializing and de-serializing a Map in JSON format.

Is a JSON a HashMap?

JSON is a text based object that different from HashMap.

What is the difference between JSONObject and JSONObject?

JSONObject is "native" to Android SDK, JsonObject is probably the one from Gson library, the one that I use. Two different package, don't work with both ;) choose one. I had some issue with the date formatting in JSONObject.

What is the use of JSONObject?

A JSONObject constructor can be used to convert an external form JSON text into an internal form whose values can be retrieved with the get and opt methods, or to convert values into a JSON text using the put and toString methods.


3 Answers

As you said, JSONObject is backed by a HashMap.

Because of this, performance will be almost identical. JSONObject.get() adds a null check, and will throw an exception if a key isn't found. JSONObject.put() just calls map.put().

So, there is almost no overhead. If you are dealing with JSON objects, you should always use JSONObject over HashMap.

like image 160
jjnguy Avatar answered Sep 21 '22 10:09

jjnguy


I would say the question doesn't make sense for a few reasons:

  1. Comparing apples to oranges: HashMap and JSONObject are intended for 2 completely different purposes. It's like asking "is the Person class or Company class more efficient for storing a PhoneNumber object". Use what makes sense.
  2. If you are converting to/from JSON, you are likely sending the data to a far away place (like a user's browser). The time taken to send this data over the network and evaluate it in the user's browser will (likely) far eclipse any performance differences of populating a Hashmap or JSONObject.
  3. There is more than 1 "JSONObject" implementation floating around out there.
  4. Finally, you haven't asked about what sort of performance you would like to measure. What are you actually planning to do with these classes?
like image 37
Mark Bolusmjak Avatar answered Sep 24 '22 10:09

Mark Bolusmjak


Existing answers are correct, performance differences between the two are negligible.

Both are basically rather inefficient methods of storing and manipulating data. More efficient method is typically to bind into regular Java objects, which use less memory and are faster to access. Many developers use org.json's simple (primitive) library because it is well-known, but it is possible the least convenient and efficient alternative available. Choices like Jackson and Gson are big improvements so it is worth considering using them.

like image 32
StaxMan Avatar answered Sep 23 '22 10:09

StaxMan