Java Reference
总结一些 Java 常用的集合接口以及一些工具类。
Java Collections 类
Collection is the root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. The JDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired.
Collection 总览
Collection | Interface | Ordered | Sorted | Thread safe | Duplicate | Nullable |
---|---|---|---|---|---|---|
ArrayList | List | Y | N | N | Y | Y |
Vector | List | Y | N | Y | Y | Y |
LinkedList | List, Deque | Y | N | N | Y | Y |
CopyOnWriteArrayList | List | Y | N | Y | Y | Y |
HashSet | Set | N | N | N | N | One null |
LinkedHashSet | Set | Y | N | N | N | One null |
TreeSet | Set | Y | Y | N | N | N |
CopyOnWriteArraySet | Set | Y | N | Y | N | One null |
ConcurrentSkipListSet | Set | Y | Y | Y | N | N |
HashMap | Map | N | N | N | N (key) | One null (key) |
HashTable | Map | N | N | Y | N (key) | N (key) |
LinkedHashMap | Map | Y | N | N | N (key) | One null (key) |
TreeMap | Map | Y | Y | N | N (key) | N (key) |
ConcurrentHashMap | Map | N | N | Y | N (key) | N |
ConcurrentSkipListMap | Map | Y | Y | Y | N (key) | N |
ArrayDeque | Deque | Y | N | N | Y | N |
PriorityQueue | Queue | Y | N | N | Y | N |
ConcurrentLinkedQueue | Queue | Y | N | Y | Y | N |
ConcurrentLinkedDeque | Deque | Y | N | Y | Y | N |
ArrayBlockingQueue | Queue | Y | N | Y | Y | N |
LinkedBlockingDeque | Deque | Y | N | Y | Y | N |
PriorityBlockingQueue | Queue | Y | N | Y | Y | N |
下面是 Collection
接口所定义的方法。
java
// 添加方法 |
三大基本接口
Collection
可以主要分为 Set
、List
、Queue
三种类型。Map
是不属于 Collection
的,Map
是一个独立的数据结构。但是 Collention
又和 Map
的实现上又互相依赖。
Queue
定义
java
public interface Queue<E> extends Collection<E>; |
方法
java
// Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available. |
Deque
定义
java
public interface Deque<E> extends Queue<E> |
方法
java
void addFirst(E e); // 添加一个元素到Deque首部,若没有空间添加则抛出IllegalStateException异常 |
List
定义
java
public interface List<E> extends Collection<E>; |
方法
java
// 修改列表 |
Set
定义
java
public interface Set<E> extends Collection<E>; |
方法
java
// 静态构造集合 下面的静态方法全部返回一个不可修改的Set |
Java 工具类
只需掌握最常用的两个工具类的使用方法即可。
Arrays
java
public static <T> List<T> asList(T... a); // 返回一个固定大小的List,包含a中所有元素 |
- 该方法适用于对象型数据的数组
- 该方法不建议使用于基本数据类型的数组
- 该方法将数组与返回的
List
连接起来,当更新其中一个时,另一个自动更新 - 不支持
add()
、remove()
、clear()
等方法
java
public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c); |
- 该方法只能用于已有序,且升序排列的数组
- 数组中的两个元素按照比较器
c
排序必须是升序的 - 可以在整个数组中查找,也可以在某个范围内查找。
java
public static <T> boolean equals(T[] a, T[] a2, Comparator<? super T> cmp); |
- 当两个数组根据比较器
cmp
比较完全相等时返回true
。
java
public static void fill(Object[] a, Object val); |
- 利用给定的
val
值去填充数组。 - 可以填充整个数组,也可以只填充部分数组。
java
public static <T> void sort(T[] a, Comparator<? super T> c); |
- 所有基本数据类型数组的底层排序算法采用
DualPivotQuicksort
,所有泛型数组的底层排序算法采用TimeSort
(归并排序算法的优化版本)。 - 有单线程和多线程的排序算法可供选择。
java
public static <T> Stream<T> stream(T[] array); |
Collections
java
public static <T> boolean addAll(Collection<? super T> c, T... elements); // 将elements添加到集合c中 |
Comparable
java
public interface Comparable<T> { |
Comparator
java
public interface Comparator<T> { |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阿日哥的向量空间!