在JAVA中如何正确的选择集合类型

程序=数据结构+算法

程序=数据结构+算法

这是编程界非常有名的一个公式,大部分程序员在刚开始接触编程的时候就会听说这个公式,我们也在持续不断的编程生涯中加深对这个公式的理解。

现在有一些观点认为这个公式过时了,觉得现在的程序包含了很多的东西,数据结构和算法已经不足以涵盖了。但我觉得这并不是公式过时,而是我们对于公式的理解需要与时俱进了。

这个中文的公式十分的贴切。数据结构就是数据的结构,这不单单指的是堆栈、数组、链表之类的东西,我们定义的实体、对象都是数据结构。算法就是通过设定好的计算步骤,对数据处理的方法,这也不单单指的排序,红黑树,递归等等,只要是对数据进行处理,对于时间的格式化展示这也是一种算法。当我们打开视角来看这些概念的时候,我们的程序还是由数据结构和算法组合而成的,只不过复杂的程序数据结构和算法在不同的层面不同的地方混合交织,有些算法是为了将一个数据结构转换为另一个,有些算法是为了修改数据结构的内容。

通常来说数据结构能分成三类:一类是单体数据结构,比如数字、字符串。第二类是复合数据结构,比如定义的实体、对象。第三类是集合数据集结构,比如列表,字典等等。

单体数据结构大多都是编程语言提供的基础类型或基础类型的扩展,比如JAVA中的String,Integer。这类数据结构在JAVA程序编写的时候就会进行声明,编译的时候进行检查,很难写错。

复合数据结构大多都是针对业务进行设定的,形成一个个的实体类,这些类都含有业务含义,只要业务抽象得当,这类对象也很难用错。

而集合数据结构本身不具有程序的业务属性,在使用上也能在一定程度上进行相互的代替,只是在不同的算法场景上哪种更合适,而如果集合数据结构用在了不合适的地方,也并不是完全用不了,但是会带来编程复杂度提升,容易产生BUG,一旦触发了BUG不好查问题等等负面影响。而JAVA提供的集合数据结构种类也不少,经常能看到有小朋友不太能分清需要用那种集合类型。今天再把以前画的图拿出来,一起来看看JAVA集合类框架提供的这些不同的集合类型,都有哪些特性,适合那些场景下使用,我们如何去选择。今天没有代码,只给工具。

JAVA的常用集合类型

JAVA常用集合类型关系图
在这里插入图片描述
上图主要列举的是常用的集合类型和他们之间的关系,并不是严格的类图,所以没有很多详细的复杂的接口、类之间的关系。另外这也并不是全部,只是列举了常用的,Queue这里要说明一下,这里并没有列出所有的队列类型,主要原因是使用率并不如其他的这么高,主要说明异步的作用,忽略同步等待队列。队列以后有机会再展开说。

OK,我们从左到右简单的了解一下,咱们这里不看代码,就看他们的特性,来源就是JDK1.8的源码注释。

List(列表)

允许重复的有序集合。

ArrayList

Resizable-array implementation of the List interface.

关键字:

array:数组结构,查询块,非有效末尾的增删慢。

Resizable:数组本身长度是不可变的,能够resize就说明要新开辟内存来做数据的来回倒换,末尾增也慢。

LinkedList

Doubly-linked list implementation of the List and Deque interfaces.

关键字:

Doubly-linked:双向链表结构,增删快,没有resize的需求,非手尾的其他元素查找慢。

Stack

The Stack class represents a last-in-first-out (LIFO) stack of objects.

关键字:

LIFO:后进先出

Queue(队列)

设计用来临时保存处理对象的集合。

BlockingQueue

BlockingQueue的JavaDoc没有对这种类型做一个概括性的介绍。

关键字

生产消费模式,适合将长的处理流程异步化。

Set(Set)

不允许重复的无序集合。

HashSet

This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

关键字:

hash table:哈希表结构,不保证顺序,增删改查快。

no guarantees as to the iteration order of the set:强调了不保证顺序。

not guarantee that the order will remain constant over time:甚至随着时间的变化顺序还有可能发生变化。

LinkedHashSet

Hash table and linked list implementation of the Set interface, with predictable iteration order. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set

关键字:

linked list:加入了链表结构,hash表还在,两个结构共同作用,性能和内存占用都稍稍逊色一点。

predictable iteration order:保证顺序性。

order in which elements were inserted into the set:元素插入的顺序。

TreeSet

A NavigableSet implementation based on a TreeMap. The elements are ordered using their Comparable natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

关键字:

TreeMap:基于TreeMap的实现,使用红黑树结构,增删时数据结构实时变化。

Comparable natural ordering:使用天然排序。

Comparator provided:或者提供一个排序方法。

Map(字典/映射)

提供一个对象和另一个对象的关系管理。

HashMap

Hash table based implementation of the Map interface. This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

关键字:

hash table:哈希表结构,不保证顺序,增删改查快。

no guarantees as to the order of the map:强调了不保证顺序。

not guarantee that the order will remain constant over time:甚至随着时间的变化顺序还有可能发生变化。

LinkedHashMap

Hash table and linked list implementation of the Map interface, with predictable iteration order. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map

关键字:

linked list:加入了链表结构,hash表还在,两个结构共同作用,性能和内存占用都稍稍逊色一点。

predictable iteration order:保证顺序性。

order in which keys were inserted into the map:元素插入的顺序。

TreeMap

A Red-Black tree based NavigableMap implementation. The map is sorted according to the Comparable natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

关键字:

Red-Black tree based:使用红黑树结构,增删时数据结构实时变化。

Comparable natural ordering:使用天然排序。

Comparator provided:或者提供一个排序方法。

编码选择场景速查表

如果在实际开发过程中遇到了需要使用这些集合结构,可以收藏下面的速查表,根据实际的编码场景选择合适的集合结构。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/cowcomic/article/details/127027118