Java HashMap 与 JSONObject

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3670586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:20:29  来源:igfitidea点击:

Java HashMap vs JSONObject

javajsonhashmap

提问by user414585

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

我想知道 Java HashMap 与 JSONObject 的性能。

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

JSONObject 似乎使用 HashMap 在内部存储数据。但与 HashMap 相比,JSONObject 可能有额外的开销。

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

有没有人知道 Java JSONObject 与 HashMap 相比的性能?

Thanks!

谢谢!

采纳答案by jjnguy

As you said, JSONObjectis backed by a HashMap.

正如你所说,JSONObjectHashMap.

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().

因此,性能将几乎相同。 JSONObject.get()添加空检查,如果未找到键,将抛出异常。 JSONObject.put()只是打电话map.put()

So, there is almost no overhead. If you are dealing with JSON objects, you should alwaysuse JSONObjectover HashMap.

因此,几乎没有开销。如果您正在处理 JSON 对象,则应始终使用JSONObjectover HashMap

回答by z5h

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?
  1. 比较苹果和橙子:HashMap 和 JSONObject 用于两个完全不同的目的。这就像问“Person 类还是 Company 类对于存储 PhoneNumber 对象更有效”。使用有意义的东西。
  2. 如果您正在转换为/从 JSON,您可能会将数据发送到很远的地方(如用户的浏览器)。通过网络发送此数据并在用户浏览器中对其进行评估所花费的时间(可能)将远远超过填充 Hashmap 或 JSONObject 的任何性能差异。
  3. 有超过 1 个“JSONObject”实现在那里浮动。
  4. 最后,您还没有询问您想要衡量什么样的绩效。你实际上打算用这些课程做什么?

回答by Rahul

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 在 HashMap 之上没有太多额外的开销。如果您可以使用 HashMap,那么您应该可以使用 JSONObject。前提是您要生成 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 检查您作为 JSONObject 的一部分存储的值的有效性,以确保它符合 JSON 规范。例如,NaN 值不构成有效 JSON 的一部分。除此之外,JSONObject 还可以生成 json 字符串(regular | prettfied)。这些字符串可能会变得非常大,具体取决于 JSON 的数量。此外,JSONObject 使用 StringBuffer,所以我要做的很多事情之一就是用 StringBuilder 替换所有出现的 StringBuffer。

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

JSONObject(来自 org.json)是您可以使用的简单 JSON 库之一。如果您想要非常高效的东西,请使用 Hymanson 之类的东西。

回答by StaxMan

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 Hymanson and Gson are big improvements so it is worth considering using them.

两者基本上都是相当低效的存储和操作数据的方法。更有效的方法通常是绑定到常规 Java 对象中,这些对象使用更少的内存并且访问速度更快。许多开发人员使用 org.json 的简单(原始)库,因为它是众所周知的,但它可能是最不方便和最有效的替代方案。像 Hymanson 和 Gson 这样的选择是很大的改进,所以值得考虑使用它们。

回答by Amir Fo

The only performance overhead is on casting data! As you JSONObjectstores data on a HashMapof objects and it casts the datatype you want.

唯一的性能开销是投射数据!当您JSONObject将数据存储在 a HashMapof 对象上时,它会转换您想要的数据类型。