The contains () method in Java

Source: Internet
Author: User

Import java.util.arraylist;import java.util.collection;/* * boolean contains (Object  o); Determines whether an element is contained in the collection.  * boolean remove (Object o); Deletes an element from the collection collection  *///contains method The underlying invocation of the book Equals method, All elements stored in the collection should override the Equals () method. Public class colletciontest02 {public static void main (String[] args) { Collection c = new arraylist (); C.add (New integer (1)); System.out.println (C.contains (1));//Add Customercustomer cus1 = new customer ("Zhangsan", 14); Customer cus2 = new customer ("Zhangsan"); C.add (CUS1); System.out.println (C.contains (CUS2));}} public class customer {private string name;private int age;public  String getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public int getage ()  {return age;} Public void setage (int age)  {this. Age = age;} Public customer (String name, int age)  {super (); this.name = name;this.age  = age;} Public customer () {}public string tostring () {return  "name="  + name + " " + "age=" + age;} Overridden the Equal method Public boolean equals (Object o) {if (this==o) {return true;} Else{if (O instanceof customer) {customer co =  (Customer) o;if (Co.name.equals (this.name) &&co.age==this.age) {return true;}}} Return false;}}


This article is from the "Gaogaozi" blog, make sure to keep this source http://hangtiangazi.blog.51cto.com/8584103/1669551

The contains () method in Java

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.