0

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.

  • What is your problem? You just log the items inside the Arraylist... You do not compare anything? – Murat Karagöz Mar 25 '15 at 12:58
  • Please clarify your question and try cutting down all this code to a minimal example of your problem. – mhlz Mar 25 '15 at 13:06
  • @Murat K. I want to get the unique, un/matched values from these two arraylists. From the above output I want to get the information as followed: – Happy Ninja Mar 25 '15 at 13:30
  • @mhlz I want to get the unique, un/matched values from these two arraylists. From the above output I want to get the information as followed: – Happy Ninja Mar 25 '15 at 13:30
  • @MuratK. I have added the output i am getting. but i also want to get other detail such as title. how can i achieve this? – Happy Ninja Mar 25 '15 at 13:38
  • @mhlz. I have added the output i am getting. but i also want to get other detail such as title. how can i achieve this? – Happy Ninja Mar 25 '15 at 13:38

1 Answers1

0

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.

Community
  • 1
  • 1
nikis
  • 11,166
  • 2
  • 35
  • 45
  • i tried with this. it does not work for me.I want to get the unique, un/matched values from these two arraylists. From the above output I want to get the information as followed: – Happy Ninja Mar 25 '15 at 13:30
  • @Happy Blue It didn't work, because by default reference equality is used. Since you are creating new objects in each loop, two objects with the same fields wouldn't be equal. You **must** implement your own structural equality to make things work. – nikis Mar 25 '15 at 13:33
  • @nikis He is actually giving the id's in the constructor. He only needs to compare the ids... – Murat Karagöz Mar 25 '15 at 13:35
  • @MuratK. I have added the output i am getting. but i also want to get other detail such as title. how can i achieve this? – Happy Ninja Mar 25 '15 at 13:37
  • @nikis still waiting for your response – Happy Ninja Mar 25 '15 at 13:51
  • @HappyBlue still the same answer: implementing structural equality will give you all what you need. – nikis Mar 25 '15 at 13:53
  • @nikis what do you mean structural equality? from my example code where and how can i implement this? can you please correct my code? or any sample code to understand? – Happy Ninja Mar 25 '15 at 13:59
  • @HappyBlue please take a look on this http://www.javapractices.com/topic/TopicAction.do?Id=17 article, for example. – nikis Mar 25 '15 at 14:03
  • @nikis it looks very difficult for me to relate with my problem – Happy Ninja Mar 25 '15 at 14:07