您好我有两个对象列表:
public class TimeTableForBus {
String bsName;
int bsType;
Time ttTime;
int lon;
int lat;
int ttID;
int bus_stop_status;
}
我生成了两个列表:
private static ArrayList getList( QueryRunner qRunner, Connection conn){
try {
beans = (List) qRunner.query(conn, "call mpklocal.LCD_GetDispInfoAllTimeTable()",
new BeanListHandler(TimeTableForBus.class));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < beans.size(); i++) {
TimeTableForBus bean = (TimeTableForBus) beans.get(i);
// bean.print();
}
ArrayList<TimeTableForBus> bus = new ArrayList<>();
for (int i = 0; i < beans.size(); i++) {
bus.add((TimeTableForBus) beans.get(i));
}
return bus;
}
当我检查是否相等时,当我这样做时,我看到了错误,我看到了错误。该列表具有相同的对象:
public static boolean equalLists(List<TimeTableForBus> one, List<TimeTableForBus> two){
if (one == null && two == null){
return true;
}
if((one == null && two != null)
|| one != null && two == null
|| one.size() != two.size()){
return false;
}
//to avoid messing the order of the lists we will use a copy
//as noted in comments by A. R. S.
// one = new ArrayList<String>(one);
// two = new ArrayList<String>(two);
//
// Collections.sort(one);
// Collections.sort(two);
return one.equals(two);
}
public static boolean listsAreEquivelent(List<? extends Object> a, List<? extends Object> b) {
if(a==null) {
if(b==null) {
//Here 2 null lists are equivelent. You may want to change this.
return true;
} else {
return false;
}
}
if(b==null) {
return false;
}
Map<Object, Integer> tempMap = new HashMap<>();
for(Object element : a) {
Integer currentCount = tempMap.get(element);
if(currentCount == null) {
tempMap.put(element, 1);
} else {
tempMap.put(element, currentCount+1);
}
}
for(Object element : b) {
Integer currentCount = tempMap.get(element);
if(currentCount == null) {
return false;
} else {
tempMap.put(element, currentCount-1);
}
}
for(Integer count : tempMap.values()) {
if(count != 0) {
return false;
}
}
return true;
}
而且我不知道为什么会有这个结果
答案 0 :(得分:0)
尝试覆盖public boolean equals(Object o)
和public int hashCode()
,如下所示:
public class TimeTableForBus {
String bsName;
int bsType;
Time ttTime;
int lon;
int lat;
int ttID;
int bus_stop_status;
@Override
public int hashCode() {
int result = 31;
result = 37 * result + generateHash(bsName);
result = 37 * result + generateHash(bsType);
result = 37 * result + generateHash(ttTime);
result = 37 * result + generateHash(lon);
result = 37 * result + generateHash(lat);
result = 37 * result + generateHash(ttID);
result = 37 * result + generateHash(bus_stop_status);
return result;
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof TimeTableForBus))
return false;
TimeTableForBus model = (TimeTableForBus)o;
return Objects.equals(bsName, model.bsName)
&& bsType == model.bsType
&& Objects.equals(ttTime, model.ttTime)
&& lon == model.lon
&& lat == model.lat
&& ttID == model.ttID
&& bus_stop_status == model.bus_stop_status;
}
private int generateHash(long value) {
return (int)(value ^ (value >>> 32));
}
private int generateHash(Object value) {
return value == null ? 0 : value.hashCode();
}
}