日期:2014-05-20 浏览次数:20770 次
import java.util.*;
public class TestCollection
{
public static void main(String[] args)
{
Collection c = new HashSet();
c.add(new Name("Yun", "Ma"));
c.add(new Name("Pengxiang", "Mei"));
c.add(new Name("Bill", "Gates"));
for (Iterator i = c.iterator();i.hasNext();)
{
Name n = (Name)i.next();
while(n.getFirstName() == "Bill")
{
i.remove();
}
}
System.out.println(c);
}
}
class Name
{
private String firstName,lastName;
public Name(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() { return firstName; }
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 (this.firstName.equals(name.firstName))
&& (this.lastName.equals(name.lastName));
}
else
{
return super.equals(obj);
}
}
public int hashCode()
{
return firstName.hashCode();
}
}