| title | date | tags | categories | ||
|---|---|---|---|---|---|
Java的集合 |
2018-11-21 02:56:52 -0800 |
|
|
💠
-
- 2.1. Apache Commons Collections
- 2.2. fastutil
- 2.3. Koloboke
- 2.4. Trove
💠 2026-01-07 15:30:18
-
Collection 接口
- List 接口
- ArrayList
- LinkedList 也实现了Queue接口 双向链表实现
- Vector
- Set 接口 内容不允许重复
- SortedSet 接口 单值排序接口
- TreeSet
- SortedSet 接口 单值排序接口
- Queue 接口 队列接口
- PiorityQueue
- Dueue 双端队列
- List 接口
-
Map接口
- HashMap 无序, key不重复
- HashTable 无序, key不重复
- TreeMap 按key排序, key不重复
- IdentityMap key可重复
- WeakHashMap 弱引用Map集合
迭代器
- 使用迭代器进行删除, 或者Java8的removeIf
- 使用没有这个特性的容器,例如: LinkedBlockingQueue
- 关联此特性的容器可以查看 java.util.ConcurrentModificationException 的JavaDoc
HashMap 键能为null, HashTable则不可以, 而且HashTable是线程安全的(依靠 synchronized 关键字实现)
interface
List接口有众多实现, 最常用的 ArrayList LinkedList
stackoverflow: list add then unsupportedoperationexception
有时候会使用 Arrays.asList() 或者 Collections.singletonList() 来快速生成 List
但是 这两个生成的实例都是返回 AbstractList 的实现类, 其 add remove 方法是没有实现的, 如果调用了就会抛出异常
public void add(int index, E element) {
throw new UnsupportedOperationException();
}这是因为, 这个类设计就是采用的定长数组来实现List, 所以不能对其中元素进行更改 类似的还有
Collections.emptyXxx()
- Set是无序的,但是StringRedisTemplate的对象操作返回的set竟然是有序的
- 因为有一个类是SortSet,顾名思义,所以是有序的,要继续多学习和使用Java原生的集合对象了
java.util.Queue ├── Deque ← 双向队列 │ ├── ArrayDeque ← 非阻塞、数组、最快 │ └── LinkedBlockingDeque ← 阻塞、链表、双端锁 ├── BlockingQueue ← 阻塞、线程安全 │ ├── ArrayBlockingQueue ← 有界、数组、单锁 │ ├── LinkedBlockingQueue ← 默认无界、链表、双锁 │ ├── SynchronousQueue ← 零容量、直接传递 │ └── PriorityBlockingQueue ← 无界、堆、优先级 └── AbstractQueue └── PriorityQueue ← 无界、堆、非阻塞
| 场景 | 一句话口诀 | 实现 |
|---|---|---|
| 单线程、最快 | “非阻塞 = ArrayDeque” | ArrayDeque |
| 多线程、生产者-消费者 | “阻塞 = LinkedBlockingQueue” | LinkedBlockingQueue |
| 优先级调度 | “优先级 = PriorityQueue” | PriorityQueue / PriorityBlockingQueue |
| 直接传递(零容量) | “Synchronous = 0 容量” | SynchronousQueue |
