【實現模式】Collections for java

來源:互聯網
上載者:User

這幾天學習了一下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方面,也正是我所缺少的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.