简体   繁体   English

LinkedHashMap订单问题

[英]LinkedHashMap order issue

It is mentioned in the LinkedHashMap Javadocs : LinkedHashMap Javadocs中提到:

In particular, operations on collection-views do not affect the order of iteration of the backing map. 特别是,对集合视图的操作不会影响后备映射的迭代顺序。

What does "operations on collection-views" mean? “收集视图上的操作”是什么意思?

The collection-views of Map s are the objects returned by: Map的集合视图是返回的对象:

  • Map#keySet()
  • Map#values()
  • Map#entrySet()

Operations on those simply means any method calls on the collection-view instance. 它们的操作只是意味着对集合视图实例的任何方法调用。

This test is supposed to demonstrate how it works 该测试应该证明它是如何工作的

    Map m = new LinkedHashMap(16, 0.75f, true);
    m.put(1, 1);
    m.put(2, 2);
    m.put(3, 3);
    System.out.println(m);
    m.get(2);
    System.out.println(m);
    Set keys = m.keySet(); //API: Returns a Set view of the keys contained in this map.
    keys.iterator().next();
    System.out.println(m);

output 产量

{1=1, 2=2, 3=3}
{1=1, 3=3, 2=2}
{1=1, 3=3, 2=2}

that is, accessing entry 2-2 changed the iteration order, and accessing the first entry 1-1 on the keySet view did not 也就是说,访问条目2-2改变了迭代顺序,并且访问keySet视图上的第一个条目1-1没有

If I understand well, maybe I don't, the collection-view are the Collection s obtained by its abstractions, like entrySet, values and keySet. 如果我理解得很好,也许我不理解,集合视图是通过其抽象获得的Collection ,如entrySet,values和keySet。

Operations in these sets will not affect the order of access of the objects inside yours LinkedHashMap when you're using the special constructor that makes your objects be order by access-order. 当您使用特殊构造函数使这些对象按访问顺序排序时,这些集合中的操作不会影响您的LinkedHashMap内部对象的访问顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM