繁体   English   中英

从两个具有Java中不同值的不同数组列表中获取所有唯一,匹配,不匹配的值

[英]Get all unique, matched, unmatched values from two different arraylist with different values in Java

从具有相同类型值的两个不同数组列表中查找非/重复值时,我遇到了奇怪的问题。

这是我的代码:

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);

这将打印出:伺服站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

您的帮助将不胜感激。

您正在寻找用于收集用户对象的交集/联合方法。 您应该做的第一步是在CustomStation对象上实现结构相等(通过覆盖equalshashcode方法)。 要找出交叉点/联合,请看一下这个问题。 另外,您可以利用Commons Collections中包含的CollectionUtils的优势。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM