Created
October 5, 2013 13:59
-
-
Save flyfire/6841265 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class HashSetTest | |
{ | |
public static void main(String[] args) | |
{ | |
HashSet hs = new HashSet(); | |
System.out.println(hs.add(new People("zhangshan"))); | |
System.out.println(hs.add(new People("lishi"))); | |
System.out.println(hs.add(new People("zhangshan"))); | |
System.out.println(hs); | |
} | |
} | |
class People | |
{ | |
String name; | |
public People(String name) | |
{ | |
this.name = name; | |
} | |
public boolean equals(Object o) | |
{ | |
if (this == o) | |
{ | |
return true; | |
} | |
else if (o instanceof People && o!=null) | |
{ | |
op = (People)o; | |
if (this.name.equals(op.name)) | |
{ | |
return true; | |
} | |
} | |
returen false; | |
} | |
public String toString() | |
{ | |
return this.name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment