2016-07-02 7 views
1

オブジェクトを一意のフィールドで塗りつぶすにはどうしたらいいですか?一意のフィールドを持つオブジェクトを追加する方法

たとえば、nameと呼ばれる一意のフィールドを持つクラスPersonがあります。したがって、重複する名前のオブジェクトを追加する場合は、追加しないでください。これをachiveする

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; 
    } 
} 
+0

可能な複製http://stackoverflow.com/questions/16009027/java-collection-of-unique-elements。 –

+0

Neria Nachum、私は、私の例ではhashCodeとequalsメソッドをオーバーライドする方法を理解できません。 – gigs

+0

それのためのGoogle。または、IDEに生成させるよう依頼し、コードを理解し、javadocで定義されているコントラクトをコンパイルする方法を理解してみてください。 –

答えて

2

つまり、その人の名前がそのアイデンティティを定義していることを意味します。あなたはそれを含めるように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(); 
    } 
} 
+1

Mureinik、迅速な対応に感謝します。あなたは私を救いました )) – gigs

1

あなたはPersonクラスにObjectクラスの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; 
    } 

} 
1

あなたのSetための独自のカスタムハッシュ戦略を定義するためにEclipse CollectionsからUnifiedSetWithHashingStrategyを使用することができます。次のコードは、あなたの例で動作します。

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 

注:私はEclipse Collectionsためのコミッターです。

関連する問題