是否有任何最佳方法可以在 2 arrayList 中找到不同的值

我正在尝试找到一种最佳方法来在其中包含映射的 2 个 arrayLists 中找到不同的元素。

前任。a1 和 a2 是数组列表,其中

  • a1 = [{"val":"1","id":"19"},{"val":"2","id":"22"},{"val":"3","id ":"2"},{"val":"4","id":"49"}]

  • a2 = [{"val":"1","id":"12"},{"val":"2","id":"22"},{"val":"3","id ":"32"},{"val":"5","id":"52"}]

预期输出为:

  • lOld = [{"val":"5","id":"52"}]

  • lNew = [{"val":"4","id":"49"}]

我对这个问题的解决方案是:

List<Map<String, String>> lOld = new ArrayList<Map<String,String>>();

for (int i = 0; i < a2.size(); i++) {

    boolean found = false;

    for (int j = 0; j < a1.size(); j++) {

        if(a2.get(i).get("val").equals(a1.get(j).get("val"))){

            found = true;

            break;

        }

    }

    if(found == false){

        lOld.add(a2.get(i));

    }

}


List<Map<String, String>> lNew = new ArrayList<Map<String,String>>();

for (int i = 0; i < a1.size(); i++) {

    boolean found = false;

    for (int j = 0; j < a2.size(); j++) {

        if(a1.get(i).get("val").equals(a2.get(j).get("val"))){

            found = true;

            break;

        }

    }

    if(found == false){

        lNew.add(a1.get(i));

    }

}

有没有解决这个问题的最佳方法?


注意:arrayList 中的映射包含多个值。a1 和 a2 仅作为示例。


茅侃侃
浏览 108回答 3
3回答

跃然一笑

将 ArrayList 转换为 HashSet这为 set.contains() 提供了 O(1)使用 Map.equals() 检查 2 个映射是否具有相同的键值对(即检查内容是否相同)现在您可以遍历一个集合并检查该元素是否存在于另一个集合中。这将为您提供线性时间而不是二次

偶然的你

解决此问题的步骤:将arrayList a1中的映射合并到映射lOld,将arrayList a2中的映射合并到映射lNew从 lOld Map 和 lNew Map 中获取 keySet将两个键集中的公共键添加到临时数组列表中从两个 keySet 中删除这个临时 arrayList。如果要以 arrayList 形式输出,请将 lOld 和 lNew 映射添加到新创建的 arrayListsIdeone 上的演示&nbsp; &nbsp; &nbsp;// start- get arrayList a1 index entries, fetch map,put all maps into one map to&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //remove duplicate keys,doesn't matter value- because they will be removed anyways)&nbsp; &nbsp; &nbsp; &nbsp; Map<String, String> lOld = new HashMap<String, String>();&nbsp; &nbsp; &nbsp; &nbsp; for (i = 0; i < a1.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HashMap<String, String> a1Map = a1.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lOld.putAll(a1Map);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //end&nbsp; &nbsp; &nbsp; &nbsp; // start- get arrayList a2 index entries, fetch map,put all maps into other map to remove&nbsp; &nbsp; &nbsp; &nbsp; // duplicate keys,doesn't matter value- because they will be removed anyways)&nbsp; &nbsp; &nbsp; &nbsp; HashMap<String, String> lNew = new HashMap<String, String>();&nbsp; &nbsp; &nbsp; &nbsp; for (j = 0; j < a2.size(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HashMap<String, String> a2Map = a2.get(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lNew.putAll(a2Map);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;//end&nbsp; &nbsp; &nbsp; &nbsp; // check if first map keys (set) is in second map keys (set).&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;//if yes, add them into a list.&nbsp; &nbsp; &nbsp; &nbsp; List<String> toRemove = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; Set<String> oldKeys = lOld.keySet();&nbsp; &nbsp; &nbsp; &nbsp; Set<String> newKeys = lNew.keySet();&nbsp; &nbsp; &nbsp; &nbsp; for (String oldKey : oldKeys) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (lNew.containsKey(oldKey)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toRemove.add(oldKey);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // remove that list elements from both sets which will remove them from map itself.&nbsp; &nbsp; &nbsp; &nbsp; oldKeys.removeAll(toRemove);&nbsp; &nbsp; &nbsp; &nbsp; newKeys.removeAll(toRemove);&nbsp; &nbsp; &nbsp; &nbsp; // print both map&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("lold map is: " + lOld);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("lNew map is: " + lNew);&nbsp; &nbsp; &nbsp; &nbsp; // don't remove elements from set while iterating. it will give ConcurrentModificationException

慕容708150

在集合中查找项目的最佳性能是使用 HashSet。在这里查看:&nbsp;https ://www.baeldung.com/java-hashset-arraylist-contains-performance
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java