Java HashMap vs JSONObject – Comparing Java HashMap and JSONObject

hashmapjavajson

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!

Best Answer

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.

Related Question