[关闭]
@xmruibi 2015-02-15T02:24:04.000000Z 字数 2722 阅读 716

Abstract Collection

Data_Structure


Collection: Interface;
AbstractCollection: Abstract Class, reduced workload of Collection interface;

// 所谓 collection骨干实现!

1. Methods Content

2. Fields:

  1. private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
  2. // still confused... why minus 8??
  3. public abstract Iterator<E> iterator();//return iterator
  4. public abstract int size();//size of container(collection)

3. Contain Check through Iterator

  1. public boolean contains(Object o) {
  2. Iterator<E> it = iterator();// Get Current Iterator
  3. if (o==null) { // Be aware of checking null value
  4. while (it.hasNext()) //
  5. if (it.next()==null)
  6. return true;
  7. } else { //Check non-null
  8. while (it.hasNext())
  9. if (o.equals(it.next()))//judge equiavalent
  10. return true;
  11. }
  12. return false;
  13. }

4. Change to Array

Return collection as an array.

  1. public Object[] toArray() {
  2. Object[] r = new Object[size()];//allocate array by size
  3. Iterator<E> it = iterator();
  4. for (int i = 0; i < r.length; i++) {
  5. if (! it.hasNext()) // end iteration if no more element
  6. return Arrays.copyOf(r, i);// to make sure the size is fit without space waste
  7. r[i] = it.next();
  8. }
  9. return it.hasNext() ? finishToArray(r, it) : r;//for thread safety
  10. }
  1. public <T> T[] toArray(T[] a) {
  2. int size = size();
  3. // make sure the collection size is bigger than a's length, otherwise generate a new array
  4. // notice the reflection to gurantee the element type
  5. T[] r = a.length >= size ? a :(T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);
  6. Iterator<E> it = iterator();
  7. for (int i = 0; i < r.length; i++) {
  8. if (! it.hasNext()) { //no more element
  9. if (a == r) {
  10. r[i] = null;
  11. } else if (a.length < i) {
  12. return Arrays.copyOf(r, i);
  13. //when the a has no enough space copy array for expand
  14. } else {
  15. System.arraycopy(r, 0, a, 0, i);
  16. if (a.length > i) {
  17. a[i] = null;
  18. }
  19. }
  20. return a;
  21. }
  22. r[i] = (T)it.next();//迭代器赋值元素
  23. }
  24. return it.hasNext() ? finishToArray(r, it) : r;
  25. }
  26. private static <T> T[] finishToArray(T[] r, Iterator<?> it) {//notice the static, but why?????
  27. int i = r.length;
  28. while (it.hasNext()) {
  29. int cap = r.length;
  30. if (i == cap) {//进行扩容
  31. int newCap = cap + (cap >> 1) + 1;//1.5 times
  32. if (newCap - MAX_ARRAY_SIZE > 0) // same step as arraylist resize?
  33. newCap = hugeCapacity(cap + 1);
  34. r = Arrays.copyOf(r, newCap);
  35. }
  36. r[i++] = (T)it.next();
  37. }
  38. return (i == r.length) ? r : Arrays.copyOf(r, i);
  39. }

5. Clear and toString

  1. public void clear() {
  2. Iterator<E> it = iterator();
  3. while (it.hasNext()) {
  4. it.next();
  5. it.remove();
  6. }
  7. }
  8. // print collection with "[" "]"?
  9. public String toString() {
  10. Iterator<E> it = iterator();
  11. if (! it.hasNext())
  12. return "[]";
  13. StringBuilder sb = new StringBuilder();
  14. sb.append('[');
  15. for (;;) {
  16. E e = it.next();
  17. sb.append(e == this ? "(this Collection)" : e);
  18. if (! it.hasNext())
  19. return sb.append(']').toString();
  20. sb.append(',').append(' ');
  21. }
  22. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注