java “插入顺序保留在集合中”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43219025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:12:28  来源:igfitidea点击:

What does it mean by "Insertion Order is preserved in Collections"?

javacollections

提问by Shashi

If we want to represent a group of individual objects where duplicates are allowed and insertion order is preserved, then we should go for List.

如果我们想表示一组允许重复并保留插入顺序的单个对象,那么我们应该选择 List。

Here, what does insertion order refers to?

这里,插入顺序指的是什么?

采纳答案by GauRang Omar

Insertion Order means the order in which we are inserting the data.

插入顺序是指我们插入数据的顺序。

public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("alpha");
    list.add("beta");
    list.add("gamma");

    for (String string : list) {
        System.out.println(string);
    }
}

Output :alpha beta gamma

输出:αβγ

Insertion order is maintained.

保持广告订单。

If you want the original insertion order there are the LinkedXXXclasses, which maintain an additional linked list in insertion order. Most of the time you don't care, so you use a HashXXX, or if you want a natural order, so you use TreeXXX.

如果您想要原始插入顺序,则可以使用 LinkedXXX类,它按插入顺序维护一个额外的链表。大多数时候你并不关心,所以你使用 Hash XXX,或者如果你想要一个自然顺序,那么你使用 Tree XXX

回答by developer

Insertion order refers to the order in which you are adding elements to the data structure (i.e., a collection like List, Set, Map, etc..).

插入顺序指的是在其中添加元素的数据结构(即,像一个集合的顺序ListSetMap等。)

For example, a Listobject maintains the order in which you are adding elements, whereas a Setobject doesn't maintain the order of the elements in which they are inserted.

例如,List对象保持添加元素的顺序,而Set对象不保持元素插入的顺序。

First, take a Listobject and add elements:

首先,取一个List对象并添加元素:

List<String> list = new ArrayList<>();
list.add("1Z");
list.add("2Y");
list.add("3X");
System.out.println(list);

Output (i.e., objects inside List): [1Z, 2Y, 3X] (order same as the insertion)

输出(即里面的对象List):[1Z, 2Y, 3X](顺序与插入相同)

Now, take a Setobject:

现在,拿一个Set对象:

Set<String> set = new HashSet<>();
set.add("1Z");
set.add("2Y");
set.add("3X");
System.out.println(set);

Output (i.e., objects inside Set): [3X, 2Y, 1Z] (order disturbed)

输出(即里面的物体Set):[3X, 2Y, 1Z](秩序被扰乱)

回答by freedev

The insertion order is the order used to add the elements in the collection.

插入顺序是用于在集合中添加元素的顺序。

Iteration order for above implementations:

上述实现的迭代顺序

  • HashSet - undefined.
  • HashMap - undefined
  • LinkedHashSet - insertion order
  • LinkedHashMap - insertion order of keys (by default), or 'access order'
  • ArrayList - insertion order.
  • LinkedList - insertion order.
  • TreeSet - ascending order, according to Comparable / Comparator.
  • HashSet - 未定义。
  • HashMap - 未定义
  • LinkedHashSet - 插入顺序
  • LinkedHashMap - 键的插入顺序(默认),或“访问顺序”
  • ArrayList - 插入顺序。
  • LinkedList - 插入顺序。
  • TreeSet - 根据 Comparable / Comparator 升序排列。

There are collection the could preserve the insertion order when you add an element, others cannot. Please refer at this link

有集合可以在添加元素时保留插入顺序,其他则不能。请参考此链接