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

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

问题描述

在 Java Collection 接口的不同实现(如 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?

推荐答案

我总是根据用例逐个做出这些决定,例如:

I've always made those decisions on a case by case basis, depending on the use case, such as:

  • 我需要保留订单吗?
  • 我会有空键/值吗?重复?
  • 是否会被多个线程访问
  • 我需要键/值对吗
  • 我需要随机访问吗?

然后我打开了我的第 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 可以解决问题,我就不会全部查找了.;) 但如果我的预期用途有任何复杂的地方,你打赌我在书中.顺便说一句,我认为 Vector 应该是老帽子"——我已经好几年没用了.

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天全站免登陆