我遇到奇怪的问题,从两个不同的数组列表中找到具有相同类型值的非/重复值。
这是我的代码:
List<CustomStation> serverStationLists = new ArrayList<CustomStation>();
List<CustomStation> localStationLists = new ArrayList<CustomStation>();
for (int i=0;i<stationResponse.size();i++) {
serverStationLists.add(new CustomStation(stationResponse.get(i).getStation_id(), stationResponse.get(i).getStation_name(),
stationResponse.get(i).getOg_station(), stationResponse.get(i).getOg_picstation()));
}
for (int j=0;j<mLocalStationList.size(); j++) {
localStationLists.add(new CustomStation(mLocalStationList.get(j).getStationId(), mLocalStationList.get(j).getTitle(),
mLocalStationList.get(j).getLikeStation(), mLocalStationList.get(j).getPicStation()));
}
System.out.println("SERVER DETAIL : " + serverStationLists);
System.out.println("LOCAL DETAIL : " + localStationLists);
for(CustomStation st : localStationLists){
System.out.println("local station id : " + st.getStationId() + " title " + st.getTitle());
}
for(CustomStation st : serverStationLists){
System.out.println("servr station id : " + st.getStationId() + " title " + st.getTitle());
}
我的模特课程:
public class CustomStation {
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
private String title;
public int getOwnerId() {
return ownerId;
}
public void setOwnerId(int ownerId) {
this.ownerId = ownerId;
}
public String getStationId() {
return stationId;
}
public void setStationId(String stationId) {
this.stationId = stationId;
}
private int ownerId;
private String stationId;
public int getStationLike() {
return stationLike;
}
public void setStationLike(int stationLike) {
this.stationLike = stationLike;
}
private int stationLike;
public int getStationPic() {
return stationPic;
}
public void setStationPic(int stationPic) {
this.stationPic = stationPic;
}
private int stationPic;
public CustomStation(String station_id, String station_title, int og_station, int og_picstation) {
super();
stationId = station_id;
title = station_title;
stationLike = og_station;
stationPic = og_picstation;
}
/*@Override
public String toString() {
return "Id : " + this.getStationId();
}
@Override
public boolean equals(Object obj) {
return (obj instanceof CustomStation) && this.getStationId() == ((CustomStation)obj).getStationId();
}*/
}
一旦我这样做,我只会获得独特的价值;但我还需要从模型类中获取其他值:
List<String> testServer = new ArrayList<String>();
List<String> testLocal = new ArrayList<String>();
for(CustomStation customStation1 : serverStationLists){
testServer.add(customStation1.getStationId());
}
for(CustomStation customStation1 : localStationLists){
testLocal.add(customStation1.getStationId());
}
List<String> tempMore = new ArrayList<String>();
tempMore.addAll(testServer);
tempMore.removeAll(testLocal);
这将打印出:servr station id:tes2119918192。
但我也希望获得更多细节,例如标题值,而不仅仅是id。我怎么能得到它?
日志输出:
local station id : tes1908252592 title check in station
local station id : tes250616810 title welcome station
local station id : tes1693047529 title Like Station 2
local station id : tes1417595199 title Like Station 3
local station id : tes1328389889 title Like Station 5
local station id : tes369097741 title check-in three
servr station id : tes1328389889 title Like Station 5
servr station id : tes1908252592 title check in station
servr station id : tes2119918192 title another station // this one is different from both array list
servr station id : tes369097741 title check-in three
从上面的输出中我想获得以下信息:
在服务器中但不在本地://唯一
servr station id : tes2119918192 title another station // this one is different from both array list
服务器和本地://匹配
tes369097741 title check-in three
tes1328389889 title Like Station 5
tes1908252592 title check in station
在本地,但在服务器://无法匹配
tes1693047529 title Like Station 2
tes1417595199 title Like Station 3
tes250616810 title welcome station
我们将不胜感激。
答案 0 :(得分:0)
您正在寻找用于收集用户对象的intersection / union方法。您应该做的第一步是在CustomStation
对象上实现结构相等(通过覆盖equals
和hashcode
方法)。要查找交集/联合,请查看this问题。或者,您可以利用CollectionUtils中包含的Commons Collections。