ListUtils.subtract (1,2)를 사용하여 다른 목록에서 하나의 값을 뺍니다.하지만 빼기가 발생하지 않았 음을 알았 기 때문에 1의 모든 요소가 계속 반환되었습니다. 나는이 평등의 문제를 표시하지만 내 해시 될 줄 알았는데 및 방법은 괜찮 같다 (내 생각)ListUtils.subtract()는 어떻게 작동합니까?
@Override
public boolean equals(Object o) {
if(!(o instanceof Car))
return false;
Car ct = (Car) o;
return this.getManufacturer().equals(ct.getManufacturer())
&& this.getImmat().equals(ct.getImmat())
&& this.getModel().equals(ct.getModel());
}
@Override
public int hashCode() {
int hash = 5;
hash = 71 * hash + (this.model != null ? this.model.hashCode() : 0);
hash = 71 * hash + (this.immat != null ? this.immat.hashCode() : 0);
hash = 71 * hash + (this.manufacturer != null ? this.manufacturer.hashCode() : 0);
return hash;
}
뺄셈을 수행하는 코드 :. 나는 두 목록을 검사 한
List <Car> affected = new LinkedList<Car>();
for(Driver c : tmp)
affected.add(c.getCar());
List <Car> allcars = carDAO.findAllCars("order by model");//returns every car
List<Car> cars = ListUtils.subtract(allcars, affected);
return cars;
및 그들은 괜찮아요,하지만 ListUtils는 allcars에서 영향을 뺀 (allcars 세트를 반환) 얻을 수 없어 어떤 이유로 equals 메서드가 잘못 될 수 있다고 생각하게합니다.
해시 코드를 대체해야한다고 생각하지 않습니다. 그냥 이것을 제거하고 다시 실행하십시오. 'while (iterator.hasNext()) { result.remove (iterator.next());를 사용합니다. }'을 빼기 위해 –
을 POJO에서 재정의했습니다. 죄송합니다. –
나는 이해한다. pojo에서 그들을 주석 처리하고 빼기를 다시 실행하여 확인하십시오. –