import java.util.ArrayList;
import java.util.Collection;
public class Test {
public static void main(String[] args) {
Collection<Object> c = new ArrayList<Object>();
c.add("hello");
c.add(new Name("f1", "l1"));
c.add(new Integer(100));
System.out.println(c.size());
System.out.println(c);
}
}
class Name {
private String firstName;
private String lastName;
Name(String firstName, String lastName) {
this.setFirstName(firstName);
this.setLastName(lastName);
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public String toString() {
return firstName + " " + lastName;
}
}
//精彩继续
import java.util.ArrayList;
import java.util.Collection;
public class Test {
public static void main(String[] args) {
Collection<Object> c = new ArrayList<Object>();
c.add("hello");
c.add(new Name("f1", "l1"));
c.add(new Integer(100));
System.out.println(c.size());
System.out.println(c);
System.out.println();
c.remove("hello");
c.remove(new Integer(100));
System.out.println(c.remove(new Name("f1", "l1")));
System.out.println(c);
}
}
class Name {
private String firstName;
private String lastName;
Name(String firstName, String lastName) {
this.setFirstName(firstName);
this.setLastName(lastName);
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public String toString() {
return firstName + " " + lastName;
}
public boolean equals(Object obj) {
if(obj instanceof Name) {
Name name = (Name) obj;
return (firstName.equals(name.firstName)) &&
(lastName.equals(name.lastName));
}
return super.equals(obj);
}
public int hashCode() {
return firstName.hashCode();
}
}
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步