【实现模式】Collections for java

这几天学习了一下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方面,也正是我所缺少的。

你可能感兴趣的:(【实现模式】Collections for java)