Java HashMap 与 JSONObject
我想知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如您所说,
JSONObject
由HashMap
支持。因此,性能几乎相同。
JSONObject.get()
添加空检查,如果未找到密钥,将引发异常。JSONObject.put()
只是调用map.put()
。因此,几乎没有任何开销。如果您正在处理 JSON 对象,则应该始终使用
JSONObject
而不是HashMap
。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
.我想说这个问题没有意义,原因如下:
I would say the question doesn't make sense for a few reasons:
现有答案是正确的,两者之间的性能差异可以忽略不计。
两者基本上都是存储和操作数据的低效方法。更有效的方法通常是绑定到常规 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.
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.
唯一的性能开销是转换数据!当您
JSONObject
将数据存储在对象的HashMap
上时,它会转换您想要的数据类型。The only performance overhead is on casting data! As you
JSONObject
stores data on aHashMap
of objects and it casts the datatype you want.