- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要将 JSONObject 转换为 Map。
我注意到 JSONObject 有一个 .toMap() 方法...
出于好奇,我深入研究了对象本身,我注意到它有一个私有(private)成员映射,其中包含 JSONObject 的映射...
但是当我查看 toMap() 方法时,我发现他们实际上创建了一个新的 Map 实例并迭代整个 JSONObject,而他们已经有了一个私有(private)成员?
所以他们在构造 JSONObject 时已经完成了工作,但是当调用 toMap() 时他们又做了一次?
为什么 toMap() 的实现并不简单
new HashMap(this.map);
?
来自 JSONObject 源代码:
/**
* The map where the JSONObject's properties are kept.
*/
private final Map<String, Object> map;
私有(private)成员几乎每个get方法都在质疑
/**
* Put a key/value pair in the JSONObject. If the value is null, then the
* key will be removed from the JSONObject if it is present.
*
* @param key
* A key string.
* @param value
* An object which is the value. It should be of one of these
* types: Boolean, Double, Integer, JSONArray, JSONObject, Long,
* String, or the JSONObject.NULL object.
* @return this.
* @throws JSONException
* If the value is non-finite number or if the key is null.
*/
public JSONObject put(String key, Object value) throws JSONException {
if (key == null) {
throw new NullPointerException("Null key.");
}
if (value != null) {
testValidity(value);
this.map.put(key, value);
} else {
this.remove(key);
}
return this;
}
在每个构造函数场景中都会调用 put 方法来填充私有(private)映射成员
/**
* Returns a java.util.Map containing all of the entries in this object.
* If an entry in the object is a JSONArray or JSONObject it will also
* be converted.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @return a java.util.Map containing the entries of this object
*/
public Map<String, Object> toMap() {
Map<String, Object> results = new HashMap<String, Object>();
for (Entry<String, Object> entry : this.entrySet()) {
Object value;
if (entry.getValue() == null || NULL.equals(entry.getValue())) {
value = null;
} else if (entry.getValue() instanceof JSONObject) {
value = ((JSONObject) entry.getValue()).toMap();
} else if (entry.getValue() instanceof JSONArray) {
value = ((JSONArray) entry.getValue()).toList();
} else {
value = entry.getValue();
}
results.put(entry.getKey(), value);
}
return results;
}
toMap创建一个新的HashMap,文学重复构造过程
我觉得他们只是无缘无故地使用一个新实例......
我错过了什么吗?
编辑:所以在阅读评论后我可以接受新实例,因为我们不希望更改引用。不过,为什么实现不那么简单
return new HashMap(this.map);
为什么要再次迭代 JSONObject?
最佳答案
Why toMap() implementation isnt simply
new HashMap(this.map);
因为这只是整个对象图的浅拷贝,因此它只包含复制的“当前”级别。
如果仔细观察,您会发现 toMap
正在对每个子对象调用 toMap
,从而创建深层复制。
关于java - JSONObject.toMap() 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56652746/
正如标题所述,将 LinkedHashMap 转换为 Map 是否会保留元素的存储顺序? 我相信不是,但找不到任何证据。 或者,在 Scala 中是否有任何不可变 Map 的实现保留了插入元素的顺序?
我正在编写一个简单的表达式,其中我必须收集 String 的 Map 与数组中的索引列表。为此,我尝试使用 Collectors.toMap(keyMapper, valueMapper, merg
我需要将 JSONObject 转换为 Map。 我注意到 JSONObject 有一个 .toMap() 方法... 出于好奇,我深入研究了对象本身,我注意到它有一个私有(private)成员映射,
我无法理解,也找不到具有 Supplied 参数的 Collectors.toMap 方法的合适示例; this one . 我完全理解其他方法,包括创建键、值、处理重复项的二进制文件的函数,我只是不
我很难在 Java 中使用流 API 来处理泛型映射。我有一张以下列方式扩展 LinkedHashMap 的 map public class LRUCache extends LinkedHashM
这个问题已经有答案了: Java 8 function that always return the same value without regarding to parameter (1 个回答)
如果我们想象,我们有一个叫做人的对象,人看起来像下面这样: class Person { int id; String name; String country //
所以可能有一个 abc 用于多次付款,现在我有: //find abc id for each payment id Map abcIdToPmtId = paymentController.find
我需要创建 Map来自 List使用 Stream API。 persons.stream() .collect(Collectors .toMap(Person
这段代码无法编译 List pairs = new ArrayList<>(); System.out.println(pairs.stream().collect(Collectors.toMap(
我的情况是 Player开发项目中的对象,任务只是测量距离并返回低于特定阈值的结果。当然,我想尽可能以最简洁的方式使用流。 目前,我有一个映射流的解决方案,然后通过迭代器进行过滤: Stream st
我正在尝试创建一个相当简单的 Collector转换 Stream进入 Map ,但是 javac 提示泛型。无法弄清楚为什么它不起作用。 import java.util.Map; import j
我有一个语言列表,如英语、法语等。我还有一个单词列表,如苹果、橙子等。对于每个单词,我想创建一个如下所示的 map : map map = {english = apple, italian = me
有一个来自 Apache 的开源 util 库,我在其中找到了一种将数组转换为映射的方法: public static Map toMap(Object[] array) { if (arra
我正在尝试在 ZipEntry 的 Stream 上使用 java8 的 Collectors.toMap。这可能不是最好的主意,因为在处理过程中可能会发生异常,但我想这应该是可能的。 我现在遇到一个
我有一个 List集合,其中 UserMeal 有: public class UserMeal { private final LocalDateTime dateTime; private f
这个问题在这里已经有了答案: How to get the key in Collectors.toMap merge function? (3 个答案) Collectors.toMap with
前言 在实际项目中我们经常会用到 List 转 Map 操作,在过去我们可能使用的是 for 循环遍历的方式。举个例子: 先定义类: ?
我从互联网上得到了这段代码,但我似乎无法理解它或在互联网上找到任何关于它的东西。 在下面的代码中 toMap 是一个返回 2 个项目的方法,这怎么可能? 什么是fromMap,它是用户创建的方法吗?我
是否有任何惯用的 JavaScript 解决方案: var addTuple = function(map,tuple) { map[tuple[0]] = tuple[1]; return map}
我是一名优秀的程序员,十分优秀!