我正在阅读LinkedHashMap
的Javadoc,其中提到:
The putAll method generates one entry access for each mapping in the
specified map, in the order that key-value mappings are provided by
the specified map’s entry set iterator.
我的问题是,它是什么意思“每个映射的一次访问权限”.如果有人能帮助提供一个例子来澄清这一点,将不胜感激.
解决方法:
本段适用于使用the special constructor创建的地图,该地图根据上次访问顺序(相对于标准LinkedHashMap的插入顺序)生成迭代顺序.
它只是说如果一个键K在地图中,你调用putAll(someOtherMap),其中someOtherMap也包含K,这将被视为对K的访问,它将被移动到地图的末尾(来自迭代顺序)透视).
换句话说,从访问角度来看,putAll等同于(条目e:条目)map.put(e); (伪代码).
举例:
public static void main(String[] args) throws Exception {
Map<String, String> m = new LinkedHashMap<> (16, 0.75f, true);
m.put("a", "a");
m.put("b", "b");
System.out.println("m = " + m); // a, b
m.put("a", "a");
System.out.println("m = " + m); // b, a
Map<String, String> m2 = new LinkedHashMap<>();
m2.put("b", "b");
m.putAll(m2);
System.out.println("m = " + m); // a, b: putAll was considered as an access
// and the order has changed
}