标签:style blog color os java io for ar div
1 package com.twoslow.cha4; 2 3 import java.util.Collection; 4 import java.util.HashSet; 5 6 /** 7 *这里我们需要扩展 HashSet 类,提供新的功能用于统计当前集合中元素的数量, 8 *实现方法是新增一个私有域变量用于保存元素数量, 9 *并每次添加新元素的方法中更新该值, 10 *再提供一个公有的方法返回该值 11 */ 12 public class InstrumentedHashSet<E> extends HashSet<E>{ 13 14 private int addCount = 0 ; 15 16 public InstrumentedHashSet(){} 17 18 public InstrumentedHashSet(int initCap , float loadFactor) { 19 super(initCap,loadFactor); 20 } 21 22 @Override 23 public boolean add(E e) { 24 addCount++ ; 25 return super.add(e) ; 26 } 27 28 /* 29 * hashSet中的addAll方法会调用add方法,计算addCount的时候会计算2次。 30 */ 31 @Override 32 public boolean addAll(Collection<? extends E> c) { 33 addCount += c.size() ; 34 return super.addAll(c); 35 } 36 37 public int getAddCount() { 38 return addCount; 39 } 40 41 42 }
修订后的设计:
1 package com.twoslow.cha4; 2 3 import java.util.Collection; 4 import java.util.Iterator; 5 import java.util.Set; 6 7 public class ForwardingSet<E> implements Set<E>{ 8 9 private final Set<E> s ; 10 11 public ForwardingSet(Set<E> s) { 12 this.s = s ; 13 } 14 15 @Override 16 public int size() { 17 return s.size(); 18 } 19 20 @Override 21 public boolean isEmpty() { 22 return s.isEmpty(); 23 } 24 25 @Override 26 public boolean contains(Object o) { 27 return s.contains(o); 28 } 29 30 @Override 31 public Iterator<E> iterator() { 32 return s.iterator(); 33 } 34 35 @Override 36 public Object[] toArray() { 37 return s.toArray(); 38 } 39 40 @Override 41 public <T> T[] toArray(T[] a) { 42 return s.toArray(a); 43 } 44 45 @Override 46 public boolean add(E e) { 47 return s.add(e); 48 } 49 50 @Override 51 public boolean remove(Object o) { 52 return s.remove(o); 53 } 54 55 @Override 56 public boolean containsAll(Collection<?> c) { 57 return s.containsAll(c); 58 } 59 60 @Override 61 public boolean addAll(Collection<? extends E> c) { 62 return s.addAll(c); 63 } 64 65 @Override 66 public boolean retainAll(Collection<?> c) { 67 return s.retainAll(c); 68 } 69 70 @Override 71 public boolean removeAll(Collection<?> c) { 72 return s.removeAll(c); 73 } 74 75 @Override 76 public void clear() { 77 s.clear(); 78 } 79 80 @Override 81 public boolean equals(Object obj) { 82 return s.equals(obj); 83 } 84 85 @Override 86 public int hashCode() { 87 return s.hashCode(); 88 } 89 90 @Override 91 public String toString() { 92 return s.toString(); 93 } 94 95 }
1 package com.twoslow.cha4; 2 3 import java.util.Collection; 4 import java.util.Set; 5 6 public class InstrumentedSet<E> extends ForwardingSet<E>{ 7 8 private int addCount = 0 ; 9 10 public InstrumentedSet(Set<E> s) { 11 super(s); 12 } 13 14 @Override 15 public boolean add(E e) { 16 addCount++ ; 17 return super.add(e); 18 } 19 20 @Override 21 public boolean addAll(Collection<? extends E> c) { 22 addCount += c.size() ; 23 return super.addAll(c); 24 } 25 26 public int getAddCount() { 27 return addCount; 28 } 29 30 31 }
标签:style blog color os java io for ar div
原文地址:http://www.cnblogs.com/twoslow/p/3938292.html