什么是java集合?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3599243/
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-08-14 02:28:01  来源:igfitidea点击:

What is a java collection?

javacollections

提问by mihir

I want to know: What is a collection in Java?

我想知道:Java 中的集合是什么?

回答by duffymo

It's a class that implements java.util.Collection interface.

它是一个实现 java.util.Collection 接口的类。

There's another branch for those that implement java.util.Map.

对于实现 java.util.Map 的那些,还有另一个分支。

These are the basis for data structures in Java: List, Set, LinkedList, HashMap, TreeMap, etc.

这些是 Java 中数据结构的基础:List、Set、LinkedList、HashMap、TreeMap 等。

回答by aioobe

Collectionis an interface in the Java API, and according to the docsit is...

Collection是 Java API 中的一个接口,根据文档,它是...

The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements.Some collections allow duplicate elements and others do not. Some are ordered and others unordered. The JDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired.

集合层次结构中的根接口。一个集合代表一组对象,称为它的元素。一些集合允许重复元素,而另一些则不允许。有些是有序的,有些是无序的。JDK 不提供此接口的任何直接实现:它提供了更具体的子接口(如 Set 和 List)的实现。此接口通常用于传递集合并在需要最大通用性的地方操作它们。

Common examples of collections are: ArrayList, HashSet, LinkedList, Stackand Vector.

收藏常见的例子是:ArrayListHashSetLinkedListStackVector

回答by Hendra Jaya

Quoting Java API"A collection — sometimes called a container — is simply an object that groups multiple elements into a single unit."

引用Java API“一个集合——有时称为容器——只是一个将多个元素组合成一个单元的对象。”

回答by Sean Patrick Floyd

Usually an instance of java.util.Collection(although java.util.Mapis officially also a part of the collections framework)

通常是java.util.Collection 的一个实例(虽然java.util.Map正式也是集合框架的一部分)

Although the Collection interface can be implemented directly, usually client code will use an implementation of one of the sub interfaces: Set, List, Queue/ Deque

尽管可以直接实现 Collection 接口,但通常客户端代码会使用以下子接口之一的实现:SetListQueue/ Deque

Here is some sample code (on the left side you will usually see an interface and on the right side an implementation class).

这是一些示例代码(在左侧,您通常会看到一个接口,而在右侧则是一个实现类)。

Setsdon't store duplicates, all of their elements are unique:

集合不存储重复项,它们的所有元素都是唯一的:

final Set<String> basicSet = new HashSet<String>();
basicSet.add("One");
basicSet.add("Two");
basicSet.add("One");
basicSet.add("Three");
System.out.println(basicSet.toString());
// Output: [Three, One, Two]
// (seemingly random order, no duplicates)

SortedSetsare a special case of sets that store elements in a specified order:

SortedSets是按指定顺序存储元素的集合的特殊情况:

final SortedSet<String> sortedSet = new TreeSet<String>();
sortedSet.add("One");
sortedSet.add("Two");
sortedSet.add("One");
sortedSet.add("Three");
System.out.println(sortedSet.toString());
// Output: [One, Three, Two]
// (natural order, no duplicates)

Listslet you store a value multiple times and access or modify insertion order:

列表让您可以多次存储一个值并访问或修改插入顺序:

final List<String> strings = new ArrayList<String>();
strings.add("Two");
strings.add("Three");
strings.add(0, "One"); // add item to beginning
strings.add(3, "One"); // add item at position 3 (zero-based)
strings.add("Three");
strings.add(strings.size() - 1, "Two"); // add item at last-but-one position
System.out.println(strings);
// Output: [One, Two, Three, One, Two, Three]

There is also a practical shorthand for defining a list:

还有一个定义列表的实用简写:

List<String> strings = Arrays.asList("One", "Two", "Three");
// this returns a different kind of list but you usually don't need to know that

etc.

等等。

To get a better understanding, read The Collections Trailfrom the Sun Java Tutorial (online), or Java Generics and Collectionsby Maurice Naftalin and Philip Wadler

要获得更好的理解,请阅读Sun Java 教程(在线)中的The Collections Trail,或Maurice Naftalin 和 Philip Wadler 的Java 泛型和集合

回答by JonWillis

I think this question is best answered in a non-programming sense.

我认为最好在非编程意义上回答这个问题。

Say you have 5 balls, and you want to move them around easily. You get a bag and place the 5 balls inside of it. The bag acts as a container. You can now move this bag around, and so quite easily the 5 balls move with it.

假设您有 5 个球,并且您想轻松地移动它们。你得到一个袋子,把 5 个球放在里面。袋子充当容器。您现在可以四处移动这个袋子,因此 5 个球很容易随之移动。

Simply put, your holding zero or more objects, inside another object for easy retrieval.

简而言之,您将零个或多个物体放在另一个物体内,以便于检索。