什么是java集合?

发布于 2024-09-16 05:19:59 字数 24 浏览 2 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

黯然#的苍凉 2024-09-23 05:19:59

通常是 java.util 的实例。集合(尽管java.util.Map 正式也是集合框架的一部分)

虽然可以直接实现 Collection 接口,但通常客户端代码将使用子接口之一的实现: 设置, 列表队列 / Deque

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

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

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)

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)

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

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]

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

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

等等。

为了更好地理解,请阅读来自 Sun Java 教程(在线)的 Collections Trail,或 Java 泛型和集合 作者:Maurice Naftalin 和 Philip Wadler

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

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

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

Sets don'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)

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

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)

Lists let 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 Trail from the Sun Java Tutorial (online), or Java Generics and Collections by Maurice Naftalin and Philip Wadler

孤芳又自赏 2024-09-23 05:19:59

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

假设您有 5 个球,并且您想轻松移动它们。你拿一个袋子,把 5 个球放进去。袋子充当容器。现在,您可以移动这个袋子,因此 5 个球很容易随之移动。

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

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.

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

唔猫 2024-09-23 05:19:59

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

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

集合的常见示例有: ArrayList, HashSet, LinkedList, 堆栈向量

Collection is an interface in the Java API, and according to the docs it is...

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.

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

牵强ㄟ 2024-09-23 05:19:59

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

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

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

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

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

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

对你的占有欲 2024-09-23 05:19:59

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文