The judgment of the set element

Source: Internet
Author: User
Tags addall set set

Determine if the sequence contains some elements:

The method of judging the list and set set is the contains method inherited from the Conllection interface.

Listjudgedemo (List judging demo):

The Contains method is called by the Equals method of each element, and if one returns True The Contains method returns True

So using the Contains method requires overriding the stored element object's Equals method

1 ImportJava.util.*;2 classListjudgedemo//List Judging Demo3 {4      Public Static voidMain (string[] args)5     {6list<courses> list =NewArraylist<courses>();7         8 AddAll (list);9         Ten         //The contains method compares a One         Booleanb = List.contains (NewCourses ("1", "Chief Technology officer"))); A System.out.println (b); -         //Containsall Method Multiple -Courses[] cc = {NewCourses ("1", "Chief Technology officer"),NewCourses ("2", "technical director")), the             NewCourses ("3", "Chief architect")}; -         BooleanB1 =List.containsall (Arrays.aslist (cc)); - System.out.println (B1); -     } +      Public Static voidAddAll (list<courses>list) -     { +Courses[] C = {NewCourses ("1", "Chief Technology officer"),NewCourses ("2", "technical director")), A             NewCourses ("3", "Chief architect")}; at List.addall (Arrays.aslist (c)); -     } -  -  - } - classCourses in { - String name; to String ID; + courses (string ID, string name) -     { the          This. ID =ID; *          This. Name =name; $     }Panax Notoginseng     //overriding the template for the Equals method -      Public Booleanequals (Object obj) the     { +         if( This==obj)//returns True if the current object equals the parameter obj A             return true; the         if(obj==NULL)//returns False if the parameter equals null +             return false; -         if(! (objinstanceofCourses))//returns False if obj is not equal to courses $             return false; $  -         //if the above compilation is passed then the current obj is a courses type object and is not null -Courses C =(courses) obj; the         //Compare by its Name property -         if( This. name==NULL)Wuyi         { the             if(c.name==NULL) -                 return true; Wu             Else -                 return false; About         } $         Else -         { -             //compare names if their names are not null -             if( This. Name.equals (c.name)) A                 return true; +             Else the                 return false; -         } $     } the}
View Code

Setjudgedemo (Set judgment Demo):

When calling HashSet's contains method, it is actually called the Hashcode method of each element to return the hash code, the hash code equal and then call its Equals method to judge, only two methods are returned true to determine the inclusion of an element

So you need to rewrite the Hashcode method and the Equals method of the object class in the set set.

There are templates to generate the Equals and Hashcode methods in Ps:eclipse, but remember to rely too much on IDE tools

The judgment of the set element

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.