Java 如何添加要设置的具有唯一字段的对象
如何用唯一字段填充对象集 例如,我有一个类Java 如何添加要设置的具有唯一字段的对象,java,set,Java,Set,如何用唯一字段填充对象集 例如,我有一个类Person,它有一个名为name的唯一字段,因此,如果我添加以设置具有重复名称的对象,则不应添加该对象 public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Set<Person> objList = new HashSet<Person>();
Person
,它有一个名为name
的唯一字段,因此,如果我添加以设置具有重复名称的对象,则不应添加该对象
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<Person> objList = new HashSet<Person>();
objList.add(new Person("John", "New York", "Accountant"));
objList.add(new Person("Bill", "London", "Manager"));
objList.add(new Person("John", "New York", "Driver"));// this object should not be added
for(Person o : objList){
System.out.println(o.name);//here should printed only John and Bill
}
}
}
class Person {
String name;//must be unique
String city;
String position;
public Person(String c_name, String c_city, String c_position){
this.name = c_name;
this.city = c_city;
this.position = c_position;
}
}
公共类测试{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
Set objList=new HashSet();
对象列表添加(新人员(“约翰”、“纽约”、“会计师”);
添加(新人员(“比尔”、“伦敦”、“经理”);
add(newperson(“John”、“newyork”、“Driver”);//不应添加此对象
对于(人员o:objList){
System.out.println(o.name);//这里应该只打印John和Bill
}
}
}
班主任{
字符串名称;//必须是唯一的
字符串城市;
串位置;
公众人物(字符串c_姓名、字符串c_城市、字符串c_职位){
this.name=c_name;
this.city=c_city;
这个位置=c_位置;
}
}
您需要超越Person
类中对象类的equals()和hashcode()方法来实现这一点。
因为您还没有为HashSet
类的Person
的所有对象执行此操作,所以HashSet将允许您添加所有Person
下面是Person类的代码片段,仅用于唯一名称
class Person{
String name;//must be unique
String city;
String position;
public Person(String c_name, String c_city, String c_position){
this.name = c_name;
this.city = c_city;
this.position = c_position;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
换句话说,你的意思是人的名字定义了他的身份。您可以通过重写
equals(Object)
和hashCode
方法来产生这样的行为,以仅包含它:
public class Person {
// members, constructors, etc.
@Override
public boolean equals(Object other) {
if (!(other instanceof Person)) {
return false;
}
return name.equals(((Person)other).name);
}
@Override
public int hashCode() {
return name.hashCode();
}
}
您可以使用from为集定义自己的自定义哈希策略。下面的代码适用于您的示例
HashingStrategy<Person> uniqueNameStrategy = new HashingStrategy<Person>() {
@Override
public int computeHashCode(Person person) {
return person.name.hashCode();
}
@Override
public boolean equals(Person person1, Person person2) {
return person1.name.equals(person2.name);
}
};
UnifiedSetWithHashingStrategy<Person> people =
UnifiedSetWithHashingStrategy.newSet(uniqueNameStrategy);
people.add(new Person("John", "New York", "Accountant"));
people.add(new Person("Bill", "London", "Manager"));
people.add(new Person("John", "New York", "Driver"));
people.each(person -> System.out.println(person.name + "/" + person.position));
// Bill/Manager
// John/Accountant
HashingStrategy uniquenamesetrategy=newhashingstrategy(){
@凌驾
公共整数计算哈希代码(个人){
返回person.name.hashCode();
}
@凌驾
公共布尔值等于(Person person1,Person person2){
返回person1.name.equals(person2.name);
}
};
统一网络与HashingStrategy人员=
统一网络与HashingStrategy.newSet(uniqueNameStrategy);
添加(新人(“约翰”、“纽约”、“会计师”);
添加(新人员(“比尔”、“伦敦”、“经理”);
添加(新人物(“约翰”、“纽约”、“司机”));
people.each(person->System.out.println(person.name+“/”+person.position));
//帐单/经理
//约翰/会计
注意:我是.Neria Nachum的提交者。可能是.Neria Nachum的副本,我不知道如何覆盖我的示例中的hashCode和equals方法。谷歌就是这样做的。或者让您的IDE为您生成它,然后尝试理解代码并理解它如何使用javadoc中定义的契约进行编译。你救了我