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 aHashMap
.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 callsmap.put()
.So, there is almost no overhead. If you are dealing with JSON objects, you should always use
JSONObject
overHashMap
.