Java 中的列表 vs 队列 vs 集合集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3940839/
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
List vs Queue vs Set of collections in Java
提问by Sumithra
what is the difference among list, queue and set?
列表、队列和集合有什么区别?
采纳答案by VoteyDisciple
In brief:
简单来说:
A listis an ordered list of objects, where the same object may well appear more than once. For example: [1, 7, 1, 3, 1, 1, 1, 5]. It makes sense to talk about the "third element" in a list. You can add an element anywhere in the list, change an element anywhere in the list, or remove an element from any position in the list.
一个列表是一个对象,在同一个对象可能出现不止一次的有序列表。例如:[1, 7, 1, 3, 1, 1, 1, 5]。谈论列表中的“第三个元素”是有道理的。您可以在列表中的任何位置添加元素,在列表中的任何位置更改元素,或从列表中的任何位置删除元素。
A queueis also ordered, but you'll only ever touch elements at one end. All elements get inserted at the "end" and removed from the "beginning" (or head) of the queue. You can find out how many elements are in the queue, but you can't find out what, say, the "third" element is. You'll see it when you get there.
一个队列也定购,但你永远只触摸元件的一端。所有元素都插入到“末尾”并从队列的“开头”(或头)中删除。您可以找出队列中有多少个元素,但您无法找出“第三个”元素是什么。当你到达那里时你会看到它。
A setis not ordered and cannot contain duplicates. Any given object either is or isn't in the set. {7, 5, 3, 1} is the exact same set as {1, 7, 1, 3, 1, 1, 1, 5}. You again can't ask for the "third" element or even the "first" element, since they are not in any particular order. You can add or remove elements, and you can find out if a certain element exists (e.g., "is 7 in this set?")
一组不排序,并不能包含重复。任何给定的对象要么在集合中,要么不在集合中。{7, 5, 3, 1} 与 {1, 7, 1, 3, 1, 1, 1, 5} 完全相同。您再次不能要求“第三个”元素甚至“第一个”元素,因为它们没有任何特定的顺序。你可以添加或删除元素,你可以找出某个元素是否存在(例如,“这个集合中有 7 个?”)