在java中从两个不同的数组列表对象中找到不常见的、常见的所有元素

问题描述:

我试图从 java 中的两个不同的无序数组列表对象中找到不常见的、常见的项目.我已经阅读了很多关于这些的帖子,但找不到正确的答案.

I am trying to find the uncommon, common items from two different non-ordered array list objects in java. I have already read many posts about these but could not find a proper answer.

第一个数组列表对象存储从服务器获取的数据.第二个数组列表对象存储本地数据库数据.

The first array list objects stores the data that are fetched from the server. The second array list objects stores the local database data.

现在我试图从这两个数组列表中找到常见的、不常见的所有元素.这里的数组列表是从完全不同的两个模型类生成的,但它们具有相似的属性.

Now I am trying to find the common, uncommon all elements from these two array lists. Here the array lists are generated from completely two different model classes but they have similar properties.

当我将条件设置为!listA.id.equals(listB.id)"时,相等比较确实给出了公共值,但无法从两个数组列表中找到不常见的项目.

The equal comparison does give the common value but can't find the uncommon items from the two array lists when i put the condition as "!listA.id.equals(listB.id)".

例如:

for(CustomStation user1 : localStationLists) {
                            for(CustomStation user2 : serverStationLists) {
                                if(user1.getStationId().equals(user2.getStationId())) {
                                    *//*if(!user1.getTitle().equals(user2.getTitle())) {
                                        resultList.add(user1);
                                    }*//*
                                    //System.out.println(" EQUAL St ids : " + user1);
                                    resultList.add(user2);
                                }
                                else{
                                    resultList1.add(user1);
                                }
                            }

那么,想一想你们是否也有同样的问题?

So, thinking whether you guys are also having the same problems?

过去三天一直在尝试使用不同的方法,但多次未能找到解决方案.

Have been trying for last three days with different approaches but have been failed repeatedly to get a solution.

我还没有测试过这段代码,但你可以试试这个:

I have not tested this code but you can try this:

for(CustomStation user1 : localStationLists) 
{
    boolean flag = false;
    for(CustomStation user2 : serverStationLists) 
    {
        if(user1.getStationId().equals(user2.getStationId())) 
        {
            if(!user1.getTitle().equals(user2.getTitle())) 
            {
                resultList.add(user1);
            }            
            resultList.add(user2);
            flag = true;
            break;
        }
    }

    if(flag == false)
    {
        resultList1.add(user1);
    }
}