我有一個程序,它從db獲取java對象的列表,並將它與已經檢索的舊列表進行比較,並找到它中的delta(差異)元素並返回。 我想知道是否有最好的方法來做到這一點,而不是僅僅使用Set方法Union(),Intersection()等,並避免內存不足的錯誤? 列表的大小可以是200k。 我在我的項目中使用Spring 3.2.8.RELEASE版本。在Java中比較兩個列表的有效方法是什麼?
public class Tester {
private List<AddressInfo> oldListOfAddresses;
@Scheduled(cron="0 1 6 * * ?") // 6 AM everyday
public Map<String, AddressInfo> getCompany() {
try {
Map<String, AddressInfo> companyMap = new HashMap<>();
String sql = "Some sql query which return Address Info.";
List<AddressInfo> newListOfAddresses = jdbcTemplate.query(sql, new Object[0],
new FacilityNewMapper());
if (newListOfAddresses == null || newListOfAddresses.size() = 0) {
throw new FacilityLookUpException("List of clinic Info from facilities is empty...");
} else {
// I have to find the delta of new list and old list here.
// I need an efficient (Space and Time) way of finding delta.
List<AddressInfo> deltaList = newListOfAddresses - oldListOfAddresses; //Something like this
for (AddressInfo comp : deltaList) {
if (comp != null) {
companyMap.put(comp.getLocationId(), comp);
}
}
oldListOfAddresses = newListOfAddresses;
}
return companyMap;
} catch (Exception e) {
throw new CompanyLookUpException(
"List of company addresses is empty..." + e.getMessage());
}
}
}
AddressInfo bean。
public class AddressInfo{
private String locationId;
private String streetName;
private String city;
private String state;
private String country;
public String getLocationId() {
return locationId;
}
public void setLocationId(String locationId) {
this.locationId = locationId;
}
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((city == null) ? 0 : city.hashCode());
result = prime * result + ((country == null) ? 0 : country.hashCode());
result = prime * result + ((locationId == null) ? 0 : locationId.hashCode());
result = prime * result + ((state == null) ? 0 : state.hashCode());
result = prime * result + ((streetName == null) ? 0 : streetName.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;
AddressInfo other = (AddressInfo) obj;
if (city == null) {
if (other.city != null)
return false;
} else if (!city.equals(other.city))
return false;
if (country == null) {
if (other.country != null)
return false;
} else if (!country.equals(other.country))
return false;
if (locationId == null) {
if (other.locationId != null)
return false;
} else if (!locationId.equals(other.locationId))
return false;
if (state == null) {
if (other.state != null)
return false;
} else if (!state.equals(other.state))
return false;
if (streetName == null) {
if (other.streetName != null)
return false;
} else if (!streetName.equals(other.streetName))
return false;
return true;
}
}
請解釋*我想知道是否有做到這一點最好的辦法,而不是僅僅使用Set方法聯盟(),交集()等,並避免內存不足錯誤?* – nullpointer
沒有「最好「 辦法。根據許多因素(列表的大小,檢索列表所需的時間,執行比較的次數等等),對於不同的情況有很好的方法。 – biziclop
您的問題不完整。您尚未指定「比較」兩個列表的含義,以及「delta」的含義。 FIrst和最重要的,注意你的'AddressInfo'類沒有定義'equals()'方法。這意味着你不能有意義地比較這個類的兩個對象,所以即使原則上也不可能做你正在問的東西。假設你提供了一個'equals()',那麼問題是列表是否可以包含重複項(基於'equals()')。那麼,你必須告訴我們,比較中元素的順序是否重要。 –