【问题标题】:Check object from one Arraylist exists in another ArrayList检查一个 Arraylist 中的对象是否存在于另一个 ArrayList 中
【发布时间】:2015-01-15 01:57:50
【问题描述】:

我正在寻找一些优化的解决方案,因为我找不到,我有两个自定义对象的 ArrayList。我想检查一个 arrayList 的特定值是否存在于另一个中。

这是我的代码

protected void updateDb(HashSet<allDataProperty> set){

    if(set != null)
    list = new ArrayList<allDataProperty>(set);

    nt = ny.getAllData();
    if(nt.size() > 0) {
        for (allData e1 : nt) {
            for (allData e2 : list) {
                if (e1.type.equals("Student") && e2.type.equals("Student")) {
                    if (!(e1.section.equals(e2.section))) {
                        Log.d(TAG, "Not FOUND" + e1.section + "dddd" + e2.section);
                    }
                } else if (e1.type.equals("Emp") && e2.type.equals("Emp")) {
                    if (!(e1.cat == (e2.cat))) {
                        Log.d(TAG, "FOUND Not" + e1.cat + "Category" + e2.cat);
                    }
                } else if (e1.type.equals("Other") && e2.type.equals("Other")) {
                    if (!(e1.cls.equals(e2.cls))) {
                        Log.d(TAG, "FOUND Not");
                    }
                } else if (e1.type.equals("OutSider") && e2.type.equals("OutSider")) {
                    if (!(e1.ran_code == (e2.ran_code))) {
                        Log.d(TAG, "FOUND Not " + e1.ran_code + "Generated" + e2.ran_code);
                    }
                }
            }
        }
    }
  }

因此,我们的想法是根据我在上面的代码中所做的某些特定条件,查找从数据库返回的列表中是否存在 ArrayList 列表中的任何值。

有没有更简单或干净的方法来做到这一点。感谢您的帮助

【问题讨论】:

  • 你不能在这里使用 ArrayList.contains(Object) 吗?见:docs.oracle.com/javase/7/docs/api/java/util/…
  • 你的意思是不遍历ArrayList,比如nt.contains(list.get(i).section ?
  • allDataProperty 是否覆盖 equalshashCode
  • 您可以使用&lt;String, allDataProperty&gt; 的映射(字符串为e1.type
  • 你能在allDataProperty课堂上发布你有什么吗

标签: java arraylist


【解决方案1】:

你可以这样做。

for(CustomObject a:listA){
    if(listB.contains(a)){
         Log.i("Activity","Found");
    }
}

【讨论】:

  • OP 还需要在 CustomObject 中实现 equals() - 否则它将默认为 Object.equals()(这可能不是所需的行为)。
【解决方案2】:

快速比较这些类型会使其飞得更快。将类型存储在变量中会使其飞得更快。我会在数据库的更高级别上执行此操作。

if(nt.size() > 0) {
    for (allData e1 : nt) {
                string mType1 = e1.type;
        for (allData e2 : list) {
                string mType2 = e2.type
            //quick compare equal
            if (mType1 == mType2) {
                if (mType1.equals("Student")) {
                    if (!(e1.section.equals(e2.section))) {
                        Log.d(TAG, "Not FOUND" + e1.section + "dddd" + e2.section);
                    }
                } else if (mType1.equals("Emp") {
                    if (!(e1.cat == (e2.cat))) {
                        Log.d(TAG, "FOUND Not" + e1.cat + "Category" + e2.cat);
                    }
                } else if (mType1.equals("Other") {
                    if (!(e1.cls.equals(e2.cls))) {
                        Log.d(TAG, "FOUND Not");
                    }
                } else if (mType1.equals("OutSider") {
                    if (!(e1.ran_code == (e2.ran_code))) {
                        Log.d(TAG, "FOUND Not " + e1.ran_code + "Generated" + e2.ran_code);
                    }
                }
            }
        }
    }
}

【讨论】:

    【解决方案3】:

    由于您已将集合作为 HashSet 传递,因此您可以利用 HashSet 可以提供的查找优势 - HashSet contains() 方法在 O(1) 或恒定时间内运行。相反,ArrayList contains() 方法需要线性时间,或 O(n) 才能运行。据我所知,您可以通过定义一个 allData.equals(allDataProperty a) 方法来做到这一点,类似于以下内容:

    //in allData class
    Boolean equals(allDataProperty a) {
        if (e1.type.equals("Student") && e2.type.equals("Student")) {
             if (e1.section.equals(e2.section)) {
                 return true;
             }
        } else if (e1.type.equals("Emp") && e2.type.equals("Emp")) {
             if (e1.cat == (e2.cat)) {
                 return true;
             }
        } else if (e1.type.equals("Other") && e2.type.equals("Other")) {
            if (e1.cls.equals(e2.cls)) {
                 return true;
            }
        } else if (e1.type.equals("OutSider") && e2.type.equals("OutSider")) {
            if (!(e1.ran_code == (e2.ran_code))) {
                return true;
            }
        } else {
            return false;
        }
    }
    

    然后您的 updateDb 函数可能如下所示: protected void updateDb(HashSet set){

        if(set != null) {
    
        for(allData e1 : nt) {
            if(set.contains(e1)) {
                Log.d("FOUND", e1);
            }
        }
    }
    

    【讨论】:

    • OP 从来没有说过任何关于速度效率的事情。只是一种“更清洁”的方式。
    猜你喜欢
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多