Java-collection framework contains () and javacontains

Source: Internet
Author: User

Java-collection framework contains () and javacontains

  • How to determine whether an element exists in a collection-contains ()

1. The contains (obj) method of List

In fact, when List calls the contains (Object obj) method, it traverses every element in the List, and then calls the equals () method of each element to compare it with the parameters in the contains () method, if the equals () method of an element returns true, the contains () method returns true. Otherwise, all equals () methods do not return true, and the ontains () method returns false. Therefore, the equals () method of the Course class is overwritten. Otherwise, the second output of the testListContains () method is false.

2. Set Contains (obj) Method

When calling the contains (Object obj) method of HashSet, The hashCode () method of each element is called first to return the hash code, if the hash code values are equal, call the equals (obj) method to determine whether the values are equal. Only when the values returned by the two methods are equal, to determine that the HashSet contains an element. Therefore, you must override the hashCode () method and equals () method of the Course class.

Run the following code to test the List and Set contains () methods:

SetTest. java

Package com. test. collection; import java. util. arrayList; import java. util. arrays; import java. util. list; import java. util. optional; public class SetTest {public List <Course> coursesToSelect; private synchronized console; public static Student student; public SetTest () {coursesToSelect = new ArrayList <Course> (); console = new instances (System. in);} public void testAdd () {Course c1 = new Course ("1", "Data Structure ");/ /Create an instance of the Course object Course c2 = new Course ("2", "C"); Course c3 = new Course ("3", "Discrete Mathematics "); course c4 = new Course ("4", "assembly language"); Course [] course = {c1, c2, c3, c4}; coursesToSelect. addAll (Arrays. asList (course);} public void testForEach () {System. out. println ("you can choose from the following courses: (For Each)"); for (Object obj: coursesToSelect) {Course c = (Course) obj; System. out. println ("Course:" + c. id + ":" + c. name );}}/*** Test the contains () method of the List */public void testListContains () {Course c = coursesToSelect. get (0); System. out. println ("Course:" + c. name); System. out. println ("course [" + c. name + "] in the Optional Course:" + coursesToSelect. contains (c); Course c2 = new Course (c. id, c. name); System. out. println ("New Course:" + c2.name); System. out. println ("course [" + c2.name + "] in optional courses:" + coursesToSelect. contains (c2); System. out. println ("Enter the course name: "); String courseName = console. next (); Course c3 = new Course (); c3.name = courseName; System. out. println ("course [" + c3.name + "] in optional courses:" + coursesToSelect. contains (c3);}/** create student and select course */public void createStudentAndSelectCourse () {Student = new student ("1", "Li Lei "); for (int I = 0; I <3; I ++) {System. out. println ("Enter the course number:"); String courseId = console. next (); for (Course c: coursesToSelect ){ If (c. id. equals (courseId) {student. courses. add (c) ;}}} public void testForEachForSet (Student student) {System. out. println ("selected" + student. courses. size () + "course! "); For (Course c: student. courses) {System. out. println ("Selected Course:" + c. id + ":" + c. name) ;}}/*** test the Set contains () method */public void testSetContains () {System. out. println ("Enter the Course name:"); String courseName = console. next (); Course c = new Course (); c. name = courseName; System. out. println ("Whether the selected course contains" + courseName + ":" + student. courses. contains (c);} public static void main (String [] args) {SetTest st = new SetTest (); st. testAdd (); st. testListContains (); st. createStudentAndSelectCourse (); st. testForEachForSet (SetTest. student); st. testSetContains ();}}

Course class:

Package com. test. collection;/*** Course class * @ author Administrator **/public class Course {public String id; public String name; public Course (String id, String name) {this. id = id; this. name = name;} public Course () {}@ Override public int hashCode () {final int prime = 31; int result = 1; result = prime * result + (name = null )? 0: name. hashCode (); return result ;}@ Override public boolean equals (Object obj) {if (this = obj) return true; if (obj = null) return false; if (! (Obj instanceof Course) return false; Course other = (Course) obj; if (name = null) {if (other. name! = Null) return false;} else if (! Name. equals (other. name) return false; return true ;}}

 

Related Article

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.