首页 > 技术文章 > contains,remove

wzd19901129 2021-03-11 20:06 原文

2021.03.11
第31次记录

**代码演示:**

```java
public class CollectionTest05 {
public static void main(String[] args) {
UserDemo u1 = new UserDemo("jack");
Collection c = new ArrayList();
c.add(u1);
UserDemo u2 = new UserDemo("jack");
System.out.println(c.contains(u2));
Collection cc = new ArrayList();
String s1 = new String("hello");
cc.add(s1);
String s2 = new String("hello");
cc.remove(s2);
System.out.println(cc.size());
}
}
class UserDemo{
private String name;
public UserDemo() {
}
public UserDemo(String name){
this.name = name;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserDemo userDemo = (UserDemo) o;
return !(name != null ? !name.equals(userDemo.name) : userDemo.name != null);
}
}
```
输出结果:
true
0

推荐阅读