2014-01-13 62 views
-2

我有一個場景,我需要從包含在集合中的對象中找到基於特定字段(在這種情況下只有顏色)的兩個交集。番石榴的Sets.Intersection方法行爲不端

因此,我試圖找到一個十字路口,通過使用谷歌的番石榴減去兩個集合(它使用比較器,使用對象的顏色來決定2個車對象的相等)。但奇怪的是,一個交叉點B不等於B交點A.

請幫我找出它出錯的地方。 爲什麼A交點B不等於B交點A?我只對交叉部分感興趣。

public class Car { 

    public String id; 
    public String color; 
    public String getId() { 
      return id; 
    } 
    public void setId(String id) { 
      this.id = id; 
    } 
    public String getColor() { 
      return color; 
    } 
    public void setColor(String color) { 
      this.color = color; 
    } 


    @Override 
    public int hashCode() { 
      final int prime = 31; 
      int result = 1; 
      result = prime * result + ((color == null) ? 0 : color.hashCode()); 
      result = prime * result + ((id == null) ? 0 : id.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; 
      Car other = (Car) obj; 
      if (color == null) { 
       if (other.color != null) 
         return false; 
      } else if (!color.equals(other.color)) 
       return false; 
      if (id == null) { 
       if (other.id != null) 
         return false; 
      } else if (!id.equals(other.id)) 
       return false; 
      return true; 
    } 
    public Car(String id, String color) { 
      super(); 
      this.id = id; 
      this.color = color; 
    } 
    public Car() { 
      super(); 
    } 


    } 

import java.util.Comparator; 
import java.util.Set; 
import java.util.TreeSet; 

import com.google.common.collect.Sets; 


public class Tester { 

    public static void main(String[] args) { 

      final Set<Car> first = new TreeSet<Car>(new Comparator<Car>(){ 
       public int compare(final Car o1, final Car o2){ 
        return comp(o1.getColor(), o2.getColor()); 
       } 
      }); 
      first.add(new Car("1","blue")); 
      first.add(new Car("2","green")); 
      first.add(new Car("3","red")); 

      final Set<Car> second = new TreeSet<Car>(new Comparator<Car>(){ 
       public int compare(final Car o3, final Car o4){ 
        return comp1(o3.getColor(), o4.getColor()); 
       } 
      }); 
      second.add(new Car("4","black")); 
      second.add(new Car("5","green")); 
      second.add(new Car("6","blue")); 
      second.add(new Car("7","red")); 

      final Set<Car> intersection1 = Sets.intersection(first, second); 
      System.out.println("intersection1 size = "+intersection1.size()); 
      for(Car carr : intersection1){ 
       System.out.println("carr.id ="+carr.id+" carr.color ="+carr.color); 
      } 
      System.out.println(); 
      final Set<Car> intersection2 = Sets.intersection(second, first); 
      System.out.println("intersection2 size = "+intersection2.size()); 
      for(Car carr : intersection2){ 
       System.out.println("carr.id ="+carr.id+" carr.color ="+carr.color); 
      } 
      System.out.println(); 

      final Set<Car> Pure1 = Sets.difference(first, second); 
      System.out.println("Pure1 size = "+Pure1.size()); 
      for(Car carr : Pure1){ 
       System.out.println("carr.id ="+carr.id+" carr.color ="+carr.color); 
      } 
      System.out.println(); 

      final Set<Car> Pure2 = Sets.difference(second, first); 
      System.out.println("Pure2 size = "+Pure2.size()); 
      for(Car carr : Pure2){ 
       System.out.println("carr.id ="+carr.id+" carr.color ="+carr.color); 
      } 
      System.out.println(); 

    } 
    static int comp(String a, String b){ 
      if(a.equalsIgnoreCase(b)){ 
       return 0; 
      } 
      else 
       return 1; 
    } 
    static int comp1(String a, String b){ 
      if(a.equalsIgnoreCase(b)){ 
       return 0; 
      } 
      else 
       return 1; 
    } 

} 

輸出:

intersection1 size = 3 
carr.id =1 carr.color =blue 
carr.id =2 carr.color =green 
carr.id =3 carr.color =red 

intersection2 size = 2 
carr.id =5 carr.color =green 
carr.id =7 carr.color =red 

Pure1 size = 0 

Pure2 size = 2 
carr.id =4 carr.color =black 
carr.id =6 carr.color =blue 
+1

你需要顯示每一組,然後將輸出的內容。請注意,「交集」與「減法」不同。 – chrylis

+1

我知道「交集」與「減法」不是一回事。你只是要求交集。沒有必要downVote – Ankit

+0

我沒有downvote,因爲我認爲這個問題可以挽救,但我們需要實際的和預期的結果。 – chrylis

回答

3

你的comp實現是不正確的,你的比較不能滿足他們的合同,這樣你就可以從TreeSet得到不確定和不可預知的行爲。這與Guava的Sets.intersection方法無關。

Comparator.compare細則中指出:

比較它的兩個參數的順序。由於第一個參數小於,等於或大於第二個參數,因此返回負整數,零或正整數。

......您的Comparator不適用。

最簡單的修復很可能會使用

new Comparator<Car>(){ 
    public int compare(Car o1, Car o2) { 
     return o1.getColor().compareToIgnoreCase(o2.getColor()); 
    } 
} 
+0

可以任何1 upvote它 – Ankit