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