【发布时间】:2017-09-03 13:30:51
【问题描述】:
我有一个程序,它从 db 获取 java 对象列表并将其与已检索到的旧列表进行比较,并在其中找到 delta(差异)元素并返回。 我想知道是否有最好的方法来做到这一点,而不仅仅是使用 Set 方法 Union()、Intersection() 等,并避免内存不足错误? 列表的大小可以是 200k。 我在我的项目中使用 Spring 3.2.8.RELEASE 版本。
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 方法 Union()、Intersection() 等,并避免内存不足错误?
-
没有“最好”的方法。对于不同的场景,有很多很好的方法,这取决于许多因素(列表的大小、检索列表所需的时间、必须执行比较的次数等等。)
-
您的问题不完整。您尚未指定“比较”两个列表的含义以及“delta”的含义。首先,也是最重要的一点,请注意您的
AddressInfo类没有定义equals()方法。这意味着您无法有意义地比较此类的两个对象,因此即使原则上也不可能按照您的要求进行。假设您提供equals(),那么问题是列表是否可以包含重复项(基于equals())。然后,您必须告诉我们元素的顺序在比较中是否重要。 -
@JimGarrison AddressInfo 类实现了
equals()和hascode(),抱歉我没有在这里提及。我所说的“delta”实际上是我希望它从两个列表中获取不同的元素。我有一个每天早上 6 点运行的调度程序,它从数据库中获取数据并存储在变量oldListOfAddresses中。因此,当调度程序在第二天运行时,我只想获取昨天列表和今天列表之间的不同元素。 -
@nagendra547 添加了 equals() 和 hashcode() 实现。
标签: java spring algorithm data-structures collections