尚学堂

来源:互联网 发布:数据集成平台架构 编辑:程序博客网 时间:2024/04/19 16:46
import java.util.*;public class BasicContainer {public static void main(String[] args) {Collection c = new HashSet();c.add("hello");c.add(new Integer(100));c.add(new Name("f1","11"));c.remove("hello");c.remove(new Integer(100));c.remove(new Name("f1","11"));System.out.println(c);}}class Name {private String firstName;private String lastName;Name(String firstName,String lastName) {this.firstName = firstName;this.lastName = lastName;} public String toString() { return firstName + " " + lastName; } public boolean equals(Object obj) {   if (obj instanceof Name) {//如果obj是Name对象        Name name = (Name) obj;//强制转换成Name        return (firstName.equals(name.firstName))            && (lastName.equals(name.lastName));    }    return super.equals(obj);//如果传进来的不是Name对象} public int hashCode() {    return firstName.hashCode();}}

0 0