简体   繁体   中英

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

I am having strange problems with finding the non/duplicate values from two different array list with same type of values.

Here is my code:

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

My Model Class:

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();
}*/
}

I am getting only unique value once i do this way; but i also need to get other values from the model class:

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

This will print out : servr station id : tes2119918192.

But i also want to get more details such as title value, not only the id. How can i get it?

Log output:

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

From the above output I want to get the information as followed:

In Server but not in Local: // unique

servr station id : tes2119918192 title another station // this one is     different from both array list

Both in Server and in Local: // matched

tes369097741 title check-in three
tes1328389889 title Like Station 5
tes1908252592 title check in station

In Local but in Server: // unmatched

tes1693047529 title Like Station 2
tes1417595199 title Like Station 3
tes250616810 title welcome station

Your help will be appreciated.

You are looking for intersection/union methods for collection of user objects. First step you should do is implementing structural equality at your CustomStation object (by overriding equals and hashcode methods). To find out intersection/union, take a look on this question. Alternatively, you can take an advantage of using CollectionUtils , which is included into Commons Collections .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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