容器深究
1.填充
- Collections.nCopies() 复制引用填充
- Collections.fill() 只作替换使用
2. 泛型便利方法
可减少使用类时所必需的类型检查数量
public class CollectionData<T> extends ArrayList<T> {
public CollectionData(Generator<T> gen, int qty){
for(int i = 0; i < qty; i ++){
add(gen.next());
}
}
// 泛型便利方法
public static <T> CollectionData<T> list(Generator<T> gen, int qty){
return new CollectionData<T>(gen, qty);
}
}
3.Map生成器
使用了各种不同的Generator、Iterator和常量值来填充Map. 具体可参见thinkingInJava中代码
4.享元模式
让对象一部分被具化;可使得在外部表中高效查找
书中例子阐释享元模式:
继承AbstractMap并实现entrySet(),创建只读map;
继承AbstractSet并实现iterator()和size(),创建只读set.
5.容器Collection接口的【添加】和【移除】方法都是可选操作。
如此设计防止在设计中出现接口爆炸;用抛UnsupportedOperationException来警示使用了不正确的接口实现
6.Set
元素唯一,必须重写equals()方法和hashcode()方法,良好的编程风格需要同时重写这2方法)
- HashSet:快速查找;要重写hashcode()
- LinkedHashSet:链表维护插入次序;重写hashcode()
- TreeSet:有序序列,树结构;要实现Comparable接口 。此实现为SortedSet的实现
7.Queue实现
- LinkedList
- PriorityQueue 对应学习代码:ToDoList—ToDoItem
- ArrayBlockingQueue
- ConcurrentLinkedQueue
- LinkedBlockingQueue
- PriorityBlockingQueue
双向队列Deque可用LinkedList实现,并不常用
8.Map
映射表,也称关联数组:维护key-value对关联,通过Key找值
- HashMap
- LinkedHashMap
- TreeMap
特殊场景的结构 - WeakHashMap
- ConcurrentHashMap
- IdentityHashMap
任何键必须具有equals()方法,用于散列是必须重写hashCode()方法,用于TreeMap时必须实现Comparable接口
(8.1)常用接口
put/get操作 | 取键/值的集合 | 判断归属 | 其他 |
---|---|---|---|
put() | keySet():返回key的Set | containsKey() | remove(key) |
get() | values():返回值组成的Collection | containsValue() | clear() |
isEmpty() | |||
putAll() | size() |
(8.2)TreeMap
排序的TreeMap是对SortedMap接口的实现,按键的次序排列
获取键 | 取子集 |
---|---|
firstKey() | subMap(fromKey, toKey) |
lastKey() | headMap(toKey) |
tailMap(fromKey) |
(8.3)LinkedHashMap
散列所有元素,但同时保持元素的插入有序(可以在构造器中设置LRU最近最少使用规则进行返回)
9. 散列与散列码
散列码:标识一个对象“唯一性”的int型值,用对象有意义的字段计算
使用散列的目的:用一个对象来快速查询另一对象
- Object.hashCode():生成散列码,默认使用对象地址计算
- Object.equals():默认比较对象的地址,Java类库中所有类重写了比较的是具体值
散列的数据结构:
- HashSet
- HashMap
- LinkedHashSet
- LinkedHashMap
对这类结构要覆盖hashCode()和equals()方法,类库中的包装类型和String类型默认重写了这两个方法。
hashCode()方法并不需要问题返回唯一的标识码;equals()方法严格判断两对象是否相等:a)比较类型; b)比较关键值
10. 为速度而散列
数组作为存储访问一组元素最快的数据结构,在map中保存着键对象的散列码(的List)
HashMap查询速度——>散列函数好坏——>数组每个位置是否有较少的散列码
eg.定义hashCode()的示例
public class CountedString{
private String s;
private int id;
…
public int hashCode(){
int result= 17;
result = 37 * result + s.hashCode();
result = 37 * result + id;
return result;
}
}
有专门生成hashCode的的工具类,Apache的Jakarta
Commons项目中有许多相应工具,该项目位于jakarta.apache.org/commons的lang下面
11. 选择接口的实现
实现类 | 特性 | 底层相关 |
---|---|---|
ArrayList | 随机访问 | 数组支持 |
LinkedList | 长于插入/删除 | 双向链表实现 |
HashSet | 查询最快,最常用 | |
LinkedHashSet | 较快速,保持元素插入有序 | |
TreeSet | 对元素排序 | |
HashMap | 查询最快,默认选择 | |
LinkedHashMap | 插入有序,迭代快 | |
TreeMap | 元素始终有序 |
12. Collections中实用方法
(12.0)常用
- addAll()
- binarySearch()
(12.1)检查容器类型
checkedCollection(Collection<T>, Class<T>type)
checkedList(List<T>, Class<T>type)
……
(12.2)取最大/最小元素
max(Collection); max(Collection, Comparator)
min(Collection); min(Collection, Comparator)
(12.3)获取索引
indexOfSubList(List source, List target)
lastIndexOfSubList(List source, List target)
(12.4)替换所有元素
replaceAll(List<T>, T oldVal, T newVal)
(12.5)逆转;逆序
reverse(List) :将元素倒转
reverseOrder() / reverseOrder(Comparator<T>)
(12.6)将后面元素轮循至前面;打乱顺序
rotate(List, int distance)
shuffle(List) / shuffle(List, Random)
(12.7)排序
sort(List<T>)
sort(List<T>, Comparator<T>)
(12.8)复制;交换
copy(List<? super T> dest, List<? extends T> src)
swap(List, int i, int j)
(12.9)复制引用;替换
nCopies(int n , T x)
fill(List<? super T>, T x) :只能在已有元素基础上进行替换
(12.10)判断是否有任何公共元素
disjoint(Collection c1, Collection c2)
(12.11)频率计算
frequency(Colleciton c, Object x) :返回Collectionk 等于x的元素个数
(12.12)返回不可变的空集
emptyList()
emptySet()
emptyMap()
13. 构造Collection/Map 只读/同步
(13.1)只读
Collection<String> data = new ArrayList<String>(Countries.names(6));
List<String> a = Collections.unmodifiableList(new ArrayList<String> data);
Set<String> s = Collections.unmodifiableSet(new HashSet<String>(data));
Set<String> ss = Collections.unmodifiableSortedSet(new TreeSet<String>(data));
…
(13.2)同步
Collection<String> c = Collections.synchronizedCollection(new ArrayList<String>());
List<String> list = Collections.synchronizedList(new ArrayList<String>());
…… 类似 synchronizedSet / synchronizedSortedSet / synchronizedMap /
synchronizedSortMap 套接在对应的实现类外面即可实现