java中需要什么集合框架?

发布于 2024-08-25 18:17:26 字数 130 浏览 18 评论 0原文

Java 中需要 Collection 框架吗?因为所有的数据操作(排序/添加/删除)都可以通过数组进行,而且数组适合内存消耗,性能也比集合更好。

任何人都可以给我指出一个实时数据导向的示例,它显示了这些实现(数组/集合)的差异。

What is the need of Collection framework in Java since all the data operations(sorting/adding/deleting) are possible with Arrays and moreover array is suitable for memory consumption and performance is also better compared with Collections.

Can anyone point me a real time data oriented example which shows the difference in both(array/Collections) of these implementations.

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

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

发布评论

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

评论(10

滥情空心 2024-09-01 18:17:26
  • 数组的大小不可调整。
  • Java Collections Framework 提供了许多不同的有用数据类型,例如链表(允许在恒定时间内插入任意位置)、可调整大小的数组列表(类似于 Vector,但更酷)、红黑树、基于哈希的映射(类似于Hashtable,但更酷)。
  • Java Collections Framework 提供了抽象,因此您可以将列表称为 List,无论是由数组列表还是链接列表支持;您可以将映射/字典称为Map,无论是由红黑树还是哈希表支持。

换句话说,Java Collections Framework 允许您使用正确的数据结构,因为一种方法并不适合所有情况。

  • Arrays are not resizable.
  • Java Collections Framework provides lots of different useful data types, such as linked lists (allows insertion anywhere in constant time), resizeable array lists (like Vector but cooler), red-black trees, hash-based maps (like Hashtable but cooler).
  • Java Collections Framework provides abstractions, so you can refer to a list as a List, whether backed by an array list or a linked list; and you can refer to a map/dictionary as a Map, whether backed by a red-black tree or a hashtable.

In other words, Java Collections Framework allows you to use the right data structure, because one size does not fit all.

失眠症患者 2024-09-01 18:17:26

有几个原因:

  • Java 的集合类提供了比数组更高级别的接口。
  • 数组有固定的大小。集合(参见 ArrayList)具有灵活的大小。
  • 在原始数组之上有效地实现复杂的数据结构(例如哈希表)是一项艰巨的任务。标准 HashMap 为您免费提供了这一点。
  • 对于同一组服务,您可以选择不同的实现:ArrayList 与 LinkedList、HashMap 与 TreeMap、同步等。
  • 最后,数组允许协变:由于键入错误,不能保证设置数组元素会成功仅在运行时才能检测到。泛型可以防止数组中出现此问题。

看一下这个说明协方差问题的片段:

  String[] strings = new String[10];
  Object[] objects = strings;

  objects[0] = new Date();  // <- ArrayStoreException: java.util.Date

Several reasons:

  • Java's collection classes provides a higher level interface than arrays.
  • Arrays have a fixed size. Collections (see ArrayList) have a flexible size.
  • Efficiently implementing a complicated data structures (e.g., hash tables) on top of raw arrays is a demanding task. The standard HashMap gives you that for free.
  • There are different implementation you can choose from for the same set of services: ArrayList vs. LinkedList, HashMap vs. TreeMap, synchronized, etc.
  • Finally, arrays allow covariance: setting an element of an array is not guaranteed to succeed due to typing errors that are detectable only at run time. Generics prevent this problem in arrays.

Take a look at this fragment that illustrates the covariance problem:

  String[] strings = new String[10];
  Object[] objects = strings;

  objects[0] = new Date();  // <- ArrayStoreException: java.util.Date
等待圉鍢 2024-09-01 18:17:26

像 Set、List 和 Map 实现这样的集合类更接近“问题空间”。它们使开发人员能够更快地完成工作并提交更具可读性/可维护性的代码。

Collection classes like Set, List, and Map implementations are closer to the "problem space." They allow developers to complete work more quickly and turn in more readable/maintainable code.

反话 2024-09-01 18:17:26

对于 Collections API 中的每个类,您的问题都有不同的答案。这里有几个例子。

LinkedList:如果从数组中间删除一个元素,则需要将所有元素移动到被删除元素的右侧。链表则不然。

集合:如果尝试使用数组实现集合,则添加元素或测试元素是否存在的时间复杂度为 O(N)。对于 HashSet,它的复杂度是 O(1)。

映射:使用数组实现映射将提供与集合的假定数组实现相同的性能特征。

For each class in the Collections API there's a different answer to your question. Here are a few examples.

LinkedList: If you remove an element from the middle of an array, you pay the cost of moving all of the elements to the right of the removed element. Not so with a linked list.

Set: If you try to implement a set with an array, adding an element or testing for an element's presence is O(N). With a HashSet, it's O(1).

Map: To implement a map using an array would give the same performance characteristics as your putative array implementation of a set.

愿与i 2024-09-01 18:17:26

这取决于您的应用程序的需求。集合的类型有很多,包括:

  • HashSet
  • ArrayList
  • HashMap
  • TreeSet
  • TreeMap
  • LinkedList

例如,如果您需要存储键/值对,如果它基于数组,则必须编写大量自定义代码 - 而Hash* 集合应该开箱即用。一如既往,为工作选择正确的工具。

It depends upon your application's needs. There are so many types of collections, including:

  • HashSet
  • ArrayList
  • HashMap
  • TreeSet
  • TreeMap
  • LinkedList

So for example, if you need to store key/value pairs, you will have to write a lot of custom code if it will be based off an array - whereas the Hash* collections should just work out of the box. As always, pick the right tool for the job.

妳是的陽光 2024-09-01 18:17:26

好吧,基本前提是“错误的”,因为 Java 自从该语言中存在接口之前就包含了 Dictionary 类……

集合提供了有点类似于数组的列表,但它们提供了更多与数组不同的东西。我假设您只是在谈论 List(甚至 Set),而将 Map 排除在外。

是的,可以通过数组获得与 List 和 Set 相同的功能,但是涉及很多工作。库的全部要点是用户不必“推出自己的”常见事物的实现。

一旦您拥有了每个人都使用的单一实现,就更容易证明花费资源对其进行优化是合理的。这意味着当标准集合加速或内存占用减少时,所有使用它们的应用程序都会免费获得改进。

每件事的单一界面也简化了每个开发人员的学习曲线 - 做同一件事没有无数种不同的方法。

如果您想要一个随时间增长的数组,您可能不会将增长代码放在整个类中,而是编写一个实用方法来实现这一点。删除和插入等也是如此...

此外,数组不太适合插入/删除,特别是当您期望 .length 成员应该反映实际内容数量时,因此您将花费大量时间来增长并缩小数组。数组也不太适合集合,因为每次想要插入以检查重复项时都必须迭代整个数组。这会破坏任何可感知的效率。

Well the basic premise is "wrong" since Java included the Dictionary class since before interfaces existed in the language...

collections offer Lists which are somewhat similar to arrays, but they offer many more things that are not. I'll assume you were just talking about List (and even Set) and leave Map out of it.

Yes, it is possible to get the same functionality as List and Set with an array, however there is a lot of work involved. The whole point of a library is that users do not have to "roll their own" implementations of common things.

Once you have a single implementation that everyone uses it is easier to justify spending resources optimizing it as well. That means when the standard collections are sped up or have their memory footprint reduced that all applications using them get the improvements for free.

A single interface for each thing also simplifies every developers learning curve - there are not umpteen different ways of doing the same thing.

If you wanted to have an array that grows over time you would probably not put the growth code all over your classes, but would instead write a single utility method to do that. Same for deletion and insertion etc...

Also, arrays are not well suited to insertion/deletion, especially when you expect that the .length member is supposed to reflect the actual number of contents, so you would spend a huge amount of time growing and shrinking the array. Arrays are also not well suited for Sets as you would have to iterate over the entire array each time you wanted to do an insertion to check for duplicates. That would kill any perceived efficiency.

不离久伴 2024-09-01 18:17:26

数组并不总是高效的。如果您需要像 LinkedList 这样的东西怎么办?看起来您需要学习一些数据结构: http://en.wikipedia.org/wiki/List_of_data_structs< /a>

Arrays are not efficient always. What if you need something like LinkedList? Looks like you need to learn some data structure : http://en.wikipedia.org/wiki/List_of_data_structures

零崎曲识 2024-09-01 18:17:26

Java Collections 提供了不同的功能、可用性和便利性。

当在应用程序中我们想要处理一组对象时,仅 ARRAY 无法帮助我们,或者更确切地说,它们可能会导致执行一些繁琐的操作。

一个重要的区别是可用性和便利性,特别是考虑到集合在需要时会自动扩展大小:

集合提出了简化我们工作的方法。

每个都有独特的功能:

  • List-本质上是一个可变大小的数组;
    您通常可以在任意位置添加/删除项目;
    项目的顺序已明确定义(即您可以说出给定项目在列表中的位置)。

    已使用 - 大多数情况下,您只需要存储或迭代“一堆东西”,然后再迭代它们。

  • 集合-事物可以“存在或不存在”——当您将项目添加到集合中时,不知道该项目被添加了多少次,通常也没有排序的概念。

    已使用-记住“您已经处理过哪些项目”,例如在进行网络抓取时;
    对某个项目做出其他是非决定,例如“该项目是英语单词吗”、“该项目是否在数据库中?” ,“该商品属于该类别吗?”等等。

在这里您可以找到每个集合根据场景的使用:

Java Collections came up with different functionality,usability and convenience.

When in an application we want to work on group of Objects, Only ARRAY can not help us,Or rather they might leads to do things with some cumbersome operations.

One important difference, is one of usability and convenience, especially given that Collections automatically expand in size when needed:

Collections came up with methods to simplify our work.

Each one has a unique feature:

  • List- Essentially a variable-size array;
    You can usually add/remove items at any arbitrary position;
    The order of the items is well defined (i.e. you can say what position a given item goes in in the list).

    Used- Most cases where you just need to store or iterate through a "bunch of things" and later iterate through them.

  • Set- Things can be "there or not"— when you add items to a set, there's no notion of how many times the item was added, and usually no notion of ordering.

    Used- Remembering "which items you've already processed", e.g. when doing a web crawl;
    Making other yes-no decisions about an item, e.g. "is the item a word of English", "is the item in the database?" , "is the item in this category?" etc.

Here you find use of each collection as per scenario:

带刺的爱情 2024-09-01 18:17:26

集合是Java中的框架,你知道框架非常容易使用,而不是实现然后使用它,你关心的是为什么我们不使用数组,数组有缺点,比如它是静态的,你必须定义行的大小至少在开始时是这样,所以如果你的数组很大,那么它主要会导致大量内存的浪费。
因此,您可以更喜欢 ArrayList 而不是集合层次结构内部的 ArrayList。

复杂性是另一个问题,就像你想插入数组一样,然后你必须跟踪它来定义索引,这样你就可以使用 LinkedList 所有函数都只在你需要使用时实现,并且变得不那么复杂,你可以读到有各种优点集合层次结构。

Collection is the framework in Java and you know that framework is very easy to use rather than implementing and then use it and your concern is that why we don't use the array there are drawbacks of array like it is static you have to define the size of row at least in beginning, so if your array is large then it would result primarily in wastage of large memory.
So you can prefer ArrayList over it which is inside the collection hierarchy.

Complexity is other issue like you want to insert in array then you have to trace it upto define index so over it you can use LinkedList all functions are implemented only you need to use and became your code less complex and you can read there are various advantages of collection hierarchy.

烦人精 2024-09-01 18:17:26

与数组相比,集合框架的级别要高得多,并提供重要的接口和类,通过使用它们,我们可以使用特定集合已经给出的许多方法,以更加复杂的方式管理对象组< /强>。

例如:

  • ArrayList - 它就像一个动态数组,即我们不需要声明它的大小,在程序运行期间,它会随着我们向其中添加元素而增大,随着我们从中删除元素而缩小。
  • LinkedList - 它可用于描述队列(FIFO)甚至堆栈(LIFO)。
  • HashSet - 它通过称为散列的过程存储其元素。 HashSet 中元素的顺序无法保证。
  • TreeSet - 当需要存储大量排序元素及其快速访问时,TreeSet 是最佳选择。
  • ArrayDeque - 它还可用于实现先进先出(FIFO)队列或后进先出(LIFO)队列。
  • HashMap - HashMap 以键值对的形式存储数据,其中键和值都是对象。
  • Treemap - TreeMap 按升序存储键值对,并且从 TreeMap 中检索元素的速度非常快。

要了解有关 Java 集合的更多信息,查看这篇文章

Collection framework are much higher level compared to Arrays and provides important interfaces and classes that by using them we can manage groups of objects with a much sophisticated way with many methods already given by the specific collection.

For example:

  • ArrayList - It's like a dynamic array i.e. we don't need to declare its size, it grows as we add elements to it and it shrinks as we remove elements from it, during the runtime of the program.
  • LinkedList - It can be used to depict a Queue(FIFO) or even a Stack(LIFO).
  • HashSet - It stores its element by a process called hashing. The order of elements in HashSet is not guaranteed.
  • TreeSet - TreeSet is the best candidate when one needs to store a large number of sorted elements and their fast access.
  • ArrayDeque - It can also be used to implement a first-in, first-out(FIFO) queue or a last-in, first-out(LIFO) queue.
  • HashMap - HashMap stores the data in the form of key-value pairs, where key and value are objects.
  • Treemap - TreeMap stores key-value pairs in a sorted ascending order and retrieval speed of an element out of a TreeMap is quite fast.

To learn more about Java collections, check out this article.

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