Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 番石榴';s集。交集方法行为异常_Java_Guava - Fatal编程技术网

Java 番石榴';s集。交集方法行为异常

Java 番石榴';s集。交集方法行为异常,java,guava,Java,Guava,我有一个场景,在这个场景中,我需要根据集合中包含的对象的特定字段(本例中仅为颜色)找到两个集合的交集 因此,我试图通过谷歌的番石榴找到一个交点,两个集的减法(使用一个比较器,它使用物体的颜色来决定两个汽车物体的相等性)。但奇怪的是,交集B不等于交集A 请帮我找出哪里出了问题。 为什么交集B不等于交集A?我只对交叉部分感兴趣 public class Car { public String id; public String color; public String

我有一个场景,在这个场景中,我需要根据集合中包含的对象的特定字段(本例中仅为颜色)找到两个集合的交集

因此,我试图通过谷歌的番石榴找到一个交点,两个集的减法(使用一个比较器,它使用物体的颜色来决定两个汽车物体的相等性)。但奇怪的是,交集B不等于交集A

请帮我找出哪里出了问题。 为什么交集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;
   }

 }

您的
comp
实现不正确,并且您的比较器不满足其契约,因此您可以从
TreeSet
获得未定义和不可预测的行为。这与番石榴的
set.intersection
方法无关

Comparator.compare
的规范说明:

比较其两个参数的顺序。返回负整数、零或正整数,因为第一个参数小于、等于或大于第二个参数

…这是您的
比较器所不具备的

最简单的修复方法可能是使用

new Comparator<Car>(){
    public int compare(Car o1, Car o2) {
        return o1.getColor().compareToIgnoreCase(o2.getColor());
    }
} 
新比较器(){
公共整数比较(o1车、o2车){
返回o1.getColor().compareToIgnoreCase(o2.getColor());
}
} 

您需要显示每组的内容,然后显示输出。注意“交叉点”和“减法”不是一回事。我知道“交叉点”和“减法”不是一回事。这是我唯一要求的十字路口。没有必要投反对票我没有投反对票,因为我认为这个问题可以挽救,但我们需要实际和预期的结果。没有必要投反对票bcoz的问题是有效的。。。
new Comparator<Car>(){
    public int compare(Car o1, Car o2) {
        return o1.getColor().compareToIgnoreCase(o2.getColor());
    }
}