Java HashMap vs JSONObject

27,652

Solution 1

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.

Solution 2

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?

Solution 3

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.

Solution 4

JSONObject does not have too much additional overhead on top of a HashMap. If you are okay with using a HashMap then you should be okay using a JSONObject. This is provided you want to generate JSON.

JSONObject checks for validity of values that you are storing as part of your JSONObject, to make sure it conforms to the JSON spec. For e.g. NaN values do not form a part of valid JSON. Apart from this, JSONObject can generate json strings (regular | prettfied). Those strings can get pretty big, depending on the amount of JSON. Also, JSONObject uses StringBuffer, so one of the many things that i would do would be to replace all occurrences of StringBuffer with StringBuilder.

JSONObject (from org.json) is one of the simple JSON libraries that you can use. If you want something very efficient, use something like Jackson.

Share:
27,652
user414585
Author by

user414585

Updated on October 01, 2020

Comments

  • user414585
    user414585 over 3 years

    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!

  • StaxMan
    StaxMan almost 14 years
    Actually I disagree with this -- since JSONObject is specific to one library, unlike Maps, exposing it as a type will add more coupling to specific library and its datatypes. So I would not categorically say it should always be used.
  • jjnguy
    jjnguy almost 14 years
    @Stax, I can see your point. But, if you need to de-couple a part of the code, you can always extract the data into a map yourself.
  • StaxMan
    StaxMan almost 14 years
    True. I guess overall it is more of a question of POJOs vs library-specific abstractions; I like to keep data format aspects closer to edges.
  • Noor Khan
    Noor Khan about 2 years
    There might be very little difference in performance but the actual thing is depend on your data and maybe on your choice. I personally use Map<String, Object> response = new HashMap<>(); . Because this is native to core java that's why I prefer it. Otherwise I tried JsonObject from google Gson library as well. Also someone can use POJO. For performance POJO will be better but doesn't fit for general.