Collections类中常用算法之Rotate
Collections类简介
Collections
类是java集合框架的一个类,其主要是一些通用的作用于Collection
的 算法,如排序,求极值,混淆(shuffle)等。
引用Java官方文档的介绍
The polymorphic algorithms described here are pieces of reusable functionality provided by the Java platform. All of them come from the Collections class, and all take the form of static methods whose first argument is the collection on which the operation is to be performed. The great majority of the algorithms provided by the Java platform operate on List instances, but a few of them operate on arbitrary Collection instances.
Collections
类的方法都是静态方法,每一种方法都对应一种集合算法的实现,且每一种实现都有两种,一种是适用于实现了RandomAccess
接口的集合类(例如ArrayList
),另一种是适用于序列存储的,例如(LinkedList
)。
Collections
类包含的算法实现大致如下:
- 排序
排序采用归并排序,所以排序算法是稳定的,时间复杂度是确定的。 - 混淆
- 常规的集合数据操作(适用于
List
)
包括reverse
、fill
、copy
、swap
、addAll
等 - 搜索(适用于
List
)
binarySearch
- 极值
求集合的最大元素、最小元素
Collections
中的大多数算法都只是适用于List
,接下来讨论的Rotate
方法就是只适用于List
的。
使用