abstractset java_AbstractSet 抽象类分析

1 packagejava.util;2 3 publicabstractclassAbstractSetextendsAbstractCollectionimplements4 Set{5 /**6 * Sole constructor. (For invocation by subclass constructors, typically7 * implicit.)8 */9 protectedAbstractSet() {10 }11 12 //Comparison and hashing13 14 /**15 * Compares the specified object with this set for equality. Returns16 * true if the given object is also a set, the two sets have the17 * same size, and every member of the given set is contained in this set.18 * This ensures that the equals method works properly across19 * different implementations of the Set interface.20 * 

21 *22 * This implementation first checks if the specified object is this set; if23 * so it returns true. Then, it checks if the specified object is a24 * set whose size is identical to the size of this set; if not, it returns25 * false. If so, it returns containsAll((Collection) o).26 *27 *@paramo28 *            object to be compared for equality with this set29 *@returntrue if the specified object is equal to this set30 */31 publicbooleanequals(Object o) {32 //是否指向同一个对象?33 if(o==this)34 returntrue;35 36 //是否都是Set类?37 if(!(oinstanceofSet))38 returnfalse;39 40 Collection c=(Collection) o;41 //先比较大小42 if(c.size()!=size())43 returnfalse;44 try{45 //再比较所有的元素46 returncontainsAll(c);47 //处理containsAll(c)抛出的异常48 }catch(ClassCastException unused) {49 returnfalse;50 }catch(NullPointerException unused) {51 returnfalse;52 }53 }54 55 /**56 * Returns the hash code value for this set. The hash code of a set is57 * defined to be the sum of the hash codes of the elements in the set, where58 * the hash code of a null element is defined to be zero. This59 * ensures that s1.equals(s2) implies that60 * s1.hashCode()==s2.hashCode() for any two sets s1 and61 * s2, as required by the general contract of62 * {@linkObject#hashCode}.63 *64 * 

65 * This implementation iterates over the set, calling the hashCode66 * method on each element in the set, and adding up the results.67 *68 *@returnthe hash code value for this set69 *@seeObject#equals(Object)70 *@seeSet#equals(Object)71 */72 publicinthashCode() {73 inth=0;74 //使用迭代器遍历所有的元素75 Iteratori=iterator();76 while(i.hasNext()) {77 E obj=i.next();78 if(obj!=null)79 //加上每个元素的hashCode80 h+=obj.hashCode();81 }82 returnh;83 }84 85 /**86 * Removes from this set all of its elements that are contained in the87 * specified collection (optional operation). If the specified collection is88 * also a set, this operation effectively modifies this set so that its89 * value is the asymmetric set difference of the two sets.90 *91 * 

92 * This implementation determines which is the smaller of this set and the93 * specified collection, by invoking the size method on each. If94 * this set has fewer elements, then the implementation iterates over this95 * set, checking each element returned by the iterator in turn to see if it96 * is contained in the specified collection. If it is so contained, it is97 * removed from this set with the iterator's remove method. If the98 * specified collection has fewer elements, then the implementation iterates99 * over the specified collection, removing from this set each element100 * returned by the iterator, using this set's remove method.101 *102 * 

103 * Note that this implementation will throw an104 * UnsupportedOperationException if the iterator returned by the105 * iterator method does not implement the remove method.106 *107 *@paramc108 *            collection containing elements to be removed from this set109 *@returntrue if this set changed as a result of the call110 *@throwsUnsupportedOperationException111 *             if the removeAll operation is not supported by this112 *             set113 *@throwsClassCastException114 *             if the class of an element of this set is incompatible with115 *             the specified collection (optional)116 *@throwsNullPointerException117 *             if this set contains a null element and the specified118 *             collection does not permit null elements (optional), or if119 *             the specified collection is null120 *@see#remove(Object)121 *@see#contains(Object)122 */123 publicbooleanremoveAll(Collection>c) {124 booleanmodified=false;125 126 //对元素少的集合使用迭代器127 if(size()>c.size()) {128 for(Iterator>i=c.iterator(); i.hasNext();)129 //Set集合删除元素,记录操作是否成功130 modified|=remove(i.next());131 }else{132 for(Iterator>i=iterator(); i.hasNext();) {133 if(c.contains(i.next())) {134 //迭代器删除元素,记录成功135 i.remove();136 modified=true;137 }138 }139 }140 returnmodified;141 }142 143 }144

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值