Java 集合:使用哪个集合以及何时使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4290132/
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
Java Collections: which collection to use and when?
提问by daydreamer
As we all know with Java comes the Collections APIthat provide us with numerous data structures that we can use.
众所周知,Java 带来了Collections API,它为我们提供了许多可以使用的数据结构。
I was wondering if there is some collection/tutorial/advice that could explain the situations and best Collection for the problem.
我想知道是否有一些收藏/教程/建议可以解释问题的情况和最佳收藏。
Example : LinkedHashMapis good for building LRU caches.
示例:LinkedHashMap非常适合构建 LRU 缓存。
回答by Tim B
I just came across this question but I recently posted a Q&A here What Java Collection should I use?that also answers this question.
我刚刚遇到了这个问题,但我最近在这里发布了一个问答,我应该使用什么 Java 集合?这也回答了这个问题。
回答by Aravind Yarram
There are enough answers provided already but I would like to augment those with few "application" areas where certain data structures are being used to solve problems
已经提供了足够的答案,但我想补充那些使用某些数据结构来解决问题的“应用程序”领域很少
List of most of the data structures can be found on wikipedia here. Most of them have a "Applications" section that describes the problem domain it is solving
大多数数据结构的列表可以在维基百科上找到。他们中的大多数都有一个“应用程序”部分,描述了它正在解决的问题域
回答by bwawok
Just reading when to use what collection will only help you if you run across the exact same situation in your code. If you don't understand the roots of why a given data structure is good for a problem, you won't be able to apply this to your own code. This is why computer science is still important in programming...
如果您在代码中遇到完全相同的情况,只需阅读何时使用什么集合才会对您有所帮助。如果您不了解为什么给定数据结构适合解决问题的根源,您将无法将其应用于您自己的代码。这就是为什么计算机科学在编程中仍然很重要的原因......
So my roundabout answer, is take data structure classes or read data structure books, and understand how they work.
所以我的迂回答案是参加数据结构课程或阅读数据结构书籍,并了解它们是如何工作的。