【问题标题】:Find the uncommon, common all elements from two different array list objects in java在java中从两个不同的数组列表对象中找到不常见的、常见的所有元素
【发布时间】:2015-05-30 19:11:59
【问题描述】:

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

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

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

当我将条件设置为“!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);
                                }
                            }

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

过去三天一直在尝试不同的方法,但一再未能找到解决方案。

【问题讨论】:

  • @Prera​​kSola 添加了示例代码。请给予适当的指导

标签: java android arraylist data-structures


【解决方案1】:

这些对我来说就像集合操作:联合、重叠和差异。

看看这个:

Classical set operations for java.util.Collection

非常适合我。代码如下:

import java.util.ArrayList;
import java.util.List;

/**
 * Add something descriptive here.
 * User: mduffy
 * Date: 3/26/2015
 * Time: 1:27 PM
 * @link https://stackoverflow.com/questions/29284061/find-the-uncommon-common-all-elements-from-two-different-array-list-objects-in/29284162?noredirect=1#comment46767251_29284162
 */
public class SetOperationDemo {

    public static void main(String[] args) {
        List<String> setOne = new ArrayList<String>() {{
            add("A");
            add("B");
            add("C");
            add("D");
            add("E");
        }};
        List<String> setTwo = new ArrayList<String>() {{
            add("D");
            add("E");
            add("F");
            add("G");
        }};

        System.out.println("Set A           : " + setOne);
        System.out.println("Set B           : " + setTwo);
        List<String> base = new ArrayList<String>(setOne);
        base.retainAll(setTwo);
        System.out.println("Intersection A+B: " + base);
        base = new ArrayList<String>(setOne);
        base.removeAll(setTwo);
        System.out.println("Subtraction  A-B: " + base);
        base = new ArrayList<String>(setTwo);
        base.removeAll(setOne);
        System.out.println("Subtraction  B-A: " + base);
        base = new ArrayList<String>(setOne);
        base.addAll(setTwo);
        System.out.println("Union A union B : " + base);
    }
}

这是输出:

Set A           : [A, B, C, D, E]
Set B           : [D, E, F, G]
Intersection A+B: [D, E]
Subtraction  A-B: [A, B, C]
Subtraction  B-A: [F, G]
Union A union B : [A, B, C, D, E, D, E, F, G]

Process finished with exit code 0

如果您的列表包含自定义类,您必须确保它们正确地覆盖 equals 和 hashCode,否则它们不会给出预期的行为。这是我使用自定义类的代码,展示了它是如何完成的。

import java.util.ArrayList;
import java.util.List;

/**
 * Add something descriptive here.
 * User: mduffy
 * Date: 3/26/2015
 * Time: 1:27 PM
 * @link https://stackoverflow.com/questions/29284061/find-the-uncommon-common-all-elements-from-two-different-array-list-objects-in/29284162?noredirect=1#comment46767251_29284162
 */
public class SetOperationDemo {

    public static void main(String[] args) {
        List<DemoPerson> setOne = new ArrayList<DemoPerson>() {{
            add(new DemoPerson("Andy", "A"));
            add(new DemoPerson("Bob", "B"));
            add(new DemoPerson("Carl", "C"));
            add(new DemoPerson("David", "D"));
            add(new DemoPerson("Ernie", "E"));
        }};
        List<DemoPerson> setTwo = new ArrayList<DemoPerson>() {{
            add(new DemoPerson("David", "D"));
            add(new DemoPerson("Ernie", "E"));
            add(new DemoPerson("Frank", "F"));
            add(new DemoPerson("Gary", "G"));
        }};

        System.out.println("Set A           : " + setOne);
        System.out.println("Set B           : " + setTwo);
        List<DemoPerson> base = new ArrayList<DemoPerson>(setOne);
        base.retainAll(setTwo);
        System.out.println("Intersection A+B: " + base);
        base = new ArrayList<DemoPerson>(setOne);
        base.removeAll(setTwo);
        System.out.println("Subtraction  A-B: " + base);
        base = new ArrayList<DemoPerson>(setTwo);
        base.removeAll(setOne);
        System.out.println("Subtraction  B-A: " + base);
        base = new ArrayList<DemoPerson>(setOne);
        base.addAll(setTwo);
        System.out.println("Union A union B : " + base);
    }
}

class DemoPerson {
    private final String firstName;
    private final String lastName;

    public DemoPerson(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        DemoPerson that = (DemoPerson) o;

        return !(firstName != null ? !firstName.equals(that.firstName) : that.firstName != null) && !(lastName != null ? !lastName.equals(that.lastName) : that.lastName != null);

    }

    @Override
    public int hashCode() {
        int result = firstName != null ? firstName.hashCode() : 0;
        result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("'").append(firstName).append('\'');
        sb.append(" '").append(lastName).append('\'');
        sb.append('}');
        return sb.toString();
    }
}

这是输出,仍然按应有的方式运行:

Set A           : [{'Andy' 'A'}, {'Bob' 'B'}, {'Carl' 'C'}, {'David' 'D'}, {'Ernie' 'E'}]
Set B           : [{'David' 'D'}, {'Ernie' 'E'}, {'Frank' 'F'}, {'Gary' 'G'}]
Intersection A+B: [{'David' 'D'}, {'Ernie' 'E'}]
Subtraction  A-B: [{'Andy' 'A'}, {'Bob' 'B'}, {'Carl' 'C'}]
Subtraction  B-A: [{'Frank' 'F'}, {'Gary' 'G'}]
Union A union B : [{'Andy' 'A'}, {'Bob' 'B'}, {'Carl' 'C'}, {'David' 'D'}, {'Ernie' 'E'}, {'David' 'D'}, {'Ernie' 'E'}, {'Frank' 'F'}, {'Gary' 'G'}]

Process finished with exit code 0

【讨论】:

  • 我刚刚发布了代码和输出。我知道它有效。你运行它并尝试过吗?
  • 查看我的示例代码。示例代码不适用于您提出的方法。找到交集,如果可以的话,从我的代码中减去..那将是一个很好的答案
  • 我不知道您的自定义类在做什么。如果您有自定义类的列表,则表明您没有正确覆盖 equals 或 hashcode。我的工作是因为 String 按预期工作。您的自定义类可能不是。问题不在于列表;这是你的代码。
【解决方案2】:

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

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

【讨论】:

  • 它会给我不相等的数据列表吗?
  • resultList1 应该有不相等的数据。
  • 你的回答给了我一些想法。到目前为止,它看起来对我来说还可以。如有其他问题,我如何与您联系?
  • 您更正的代码目前运行良好。谢谢 但是还有一个小问题。如何从第一个相等的检查条件中获取不相等的 user2.getStationId() id? ..if(user1.getStationId().equals(user2.getStationId())) {...
  • 我不明白。你能详细说明一下吗?
猜你喜欢
  • 2020-06-16
  • 2012-07-06
  • 2018-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
相关资源
最近更新 更多