這幾天學習了一下Kent Beck的《實現模式(Implementation Patterns)》,可能是由於剛讀完《Design Patterns》的原因,覺得作者的語言使用太過於隨意話,《design patterns》的邏輯性更勝一籌。
所謂的實現模式,也不乏一些編程技巧,從設計模式的角度來看,只不過是一些trick而已。
而且書中好多地方比較牽強。可能由於我不是java程式員的緣故,書中好多內容不能理解,但是還是堅持看了下來。
細讀了collections一章並做如下筆記。
1 Collections hover in a strange world halfway between a programming language construct and a library.
其實從C到C++發展中可以看出,C中的collection都是需要通過construct才可得到,C++中的STL已經成了first-class language element.
2 metaphors.
- The first is that of a multi-valued variable.
- The second metaphor mixed into collections is that of objects --- a collection is an object.
- A third metaphor useful for thinking about collections is that of mathematical sets.
3 Issues
- The first concept expressed by collections is their size.
- A second concept expressed through collections is whether or not the order of elements is important.
- Another issue to be expressed by collections is the uniqueness of elements.
4 Interfaces
android好像多了一個SparseArray,等價於Map<Integer, Object>,Performance的區別。
- Array
- Iterable
- Collection
- List
- Set
- SortedSet
- Map
5 Implementations
As with all performance issues, it is best to pick a simple implementation to begin with and then tune based on experience. If you see a profile dominated by calls to add() or remove(), consider switching an ArrayList to
a LinkedList.
6 Collections
- Searching
- Call Collections.binarySearch(list, element) to return the index of an element in the list
- Sorting
- Shuffle(list), Reverse(list), Sort(list), sort(list, comparator)
- Unlike binary search, sorting performance is roughly the same for ArrayList and LinkedList, because the elements are first copied into an array, the array is sorted, and then the elements are copied back.
- Unmodifiable Collections
- Collections.unmodifiableCollection()
- Single-Element Collections
- Collections.singleton()
7 Extending Collections
Adapter
Java內的Collection功能還是比較強大的,可以在不同場合使用不同的Collection。
這也是我在android代碼沒有意識到的問題,基本上都是使用ArrayList,雖然資料量比較少。
所以說效能問題的改善,還是需要具備較深的語言功底,而在java方面,也正是我所缺少的。