Java HashMap 与 JSONObject

发布于 2024-09-17 21:26:09 字数 180 浏览 3 评论 0原文

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

看起来 JSONObject 在内部使用 HashMap 存储数据。但与 HashMap 相比,JSONObject 可能会产生额外的开销。

有人知道 Java JSONObject 与 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寻找一个思念的角度 2024-09-24 21:26:09

正如您所说,JSONObjectHashMap 支持。

因此,性能几乎相同。 JSONObject.get() 添加空检查,如果未找到密钥,将引发异常。 JSONObject.put() 只是调用 map.put()

因此,几乎没有任何开销。如果您正在处理 JSON 对象,则应该始终使用 JSONObject 而不是 HashMap

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.

云淡风轻 2024-09-24 21:26:09

我想说这个问题没有意义,原因如下:

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

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?
动次打次papapa 2024-09-24 21:26:09

现有答案是正确的,两者之间的性能差异可以忽略不计。

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

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.

我的奇迹 2024-09-24 21:26:09

JSONObject 在 HashMap 之上没有太多额外的开销。如果您可以使用 HashMap,那么您应该可以使用 JSONObject。这是假设您想要生成 JSON。

JSONObject 检查您存储为 JSONObject 一部分的值的有效性,以确保它符合 JSON 规范。例如,NaN 值不构成有效 JSON 的一部分。除此之外,JSONObject 还可以生成 json 字符串(常规 | prettfied)。这些字符串可能会变得相当大,具体取决于 JSON 的数量。另外,JSONObject 使用 StringBuffer,因此我要做的许多事情之一就是用 StringBuilder 替换所有出现的 StringBuffer。

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

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.

南街九尾狐 2024-09-24 21:26:09

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

The only performance overhead is on casting data! As you JSONObject stores data on a HashMap of objects and it casts the datatype you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文