Abstract set inherits abstract collections, but overrides the equal and Hashcode methods in addition
Abstract set is an extension of the set interface
Public abstract class Abstractset<e> extends abstractcollection<e> implements set<e> {
Protected Abstractset () {
}
Two sets are equal in comparison, compare whether the reference is the same, or continue to compare the size is consistent, and then to two of each element in the set one by one comparison is equal
public boolean equals (Object o) {
if (o = = this)
return true;
if (!) ( o instanceof Set))
return false;
Collection C = (Collection) o;
if (c.size ()!= size ())
return false;
try {
return Containsall (c);
catch (ClassCastException unused) {
return false;
catch (NullPointerException unused) {
return false;
}
}
The Hashcode calculation method for all classes that inherit this abstract set:
is to add the hashcode value of each element in the set
public int hashcode () {
int h = 0;
iterator<e> i = iterator ();
while (I.hasnext ()) {
E obj = I.next ();
if (obj!= null)
H + + obj.hashcode ();
}
return h;
}
If the current set size is greater than the incoming set, then the passed in argument is removed in the current set as the traversal factor
Otherwise, the current set size is used as the traversal factor to remove the operation.
That is, whose size is smaller, and who is the traversal factor to remove operations from another set
public boolean RemoveAll (collection<?> c) {
Boolean modified = false;
if (size () > C.size ()) {
for (Iterator<?> i = C.iterator (); I.hasnext ();)
Modified |= Remove (I.next ());
} else {
for (iterator<?> i = iterator (); I.hasnext ();) {
if (C.contains (I.next ())) {
I.remove ();
Modified = true;
}
}
}
return modified;
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service