选择Java集合的实现的经验法则? [英] Rule of thumb for choosing an implementation of a Java Collection?

查看:142
本文介绍了选择Java集合的实现的经验法则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都有一个很好的经验法则,可以在不同的实现之间进行选择,如List,Map或Set?

Anyone have a good rule of thumb for choosing between different implementations of Java Collection interfaces like List, Map, or Set?

例如,一般为什么或在什么我更喜欢使用Vector或者ArrayList,Hashtable或者HashMap?

For example, generally why or in what cases would I prefer to use a Vector or an ArrayList, a Hashtable or a HashMap?

推荐答案

取决于用例,例如:


  • 我需要订购吗?

  • 我有空键/值吗?

  • 是否需要键/值对

  • 我需要随机存取吗?

  • Do I need the ordering to remain?
  • Will I have null key/values? Dups?
  • Will it be accessed by multiple threads
  • Do I need a key/value pair
  • Will I need random access?

然后,我突破了我的方便的第5版 Java in a Nutshell 比较〜20左右的选项。它在第五章有很好的小表,以帮助一个人找出什么是适当的。

And then I break out my handy 5th edition Java in a Nutshell and compare the ~20 or so options. It has nice little tables in Chapter five to help one figure out what is appropriate.

好吧,也许如果我知道一个简单的ArrayList或HashSet将执行的技巧,我不会看起来一切。 ;)但是如果有任何关于我的使用的远程复杂,你打赌我在书中。 BTW,我虽然矢量应该是老帽子 - 我几年没有使用。

Ok, maybe if I know off the cuff that a simple ArrayList or HashSet will do the trick I won't look it all up. ;) but if there is anything remotely complex about my indended use, you bet I'm in the book. BTW, I though Vector is supposed to be 'old hat'--I've not used on in years.

这篇关于选择Java集合的实现的经验法则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆