java中需要什么集合框架?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
Hashtable
,但更酷)。List
,无论是由数组列表还是链接列表支持;您可以将映射/字典称为Map
,无论是由红黑树还是哈希表支持。换句话说,Java Collections Framework 允许您使用正确的数据结构,因为一种方法并不适合所有情况。
Vector
but cooler), red-black trees, hash-based maps (likeHashtable
but cooler).List
, whether backed by an array list or a linked list; and you can refer to a map/dictionary as aMap
, 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.
有几个原因:
看一下这个说明协方差问题的片段:
Several reasons:
Take a look at this fragment that illustrates the covariance problem:
像 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.
对于 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.
这取决于您的应用程序的需求。集合的类型有很多,包括:
例如,如果您需要存储键/值对,如果它基于数组,则必须编写大量自定义代码 - 而Hash* 集合应该开箱即用。一如既往,为工作选择正确的工具。
It depends upon your application's needs. There are so many types of collections, including:
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.
好吧,基本前提是“错误的”,因为 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.
数组并不总是高效的。如果您需要像
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_structuresJava 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:
集合是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.
与数组相比,集合框架的级别要高得多,并提供重要的接口和类,通过使用它们,我们可以使用特定集合已经给出的许多方法,以更加复杂的方式管理对象组< /强>。
例如:
要了解有关 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:
To learn more about Java collections, check out this article.