有效复制或合并包含相同对象的两个ArrayList的方法

时间:2017-12-05 16:02:55

标签: java android collections

我有一个像下面的对象

Product {

String name;

boolean isFiltered;

}

我们有两个列表 - 包含product的masterProductList和filteredProductList。

我们必须将filteredProductList的值(此列表具有属性isFiltered set)复制到masterProductList。

我可以通过以下方法来实现 -

方法1

for (Product filterProduct: filteredPL) {
    for (Product masterProduct: masterPL) {
        if (filterProduct.equals(masterProduct)) {
            // Copy Values
            masterProduct.setIsFiltered(filterProduct.getIsFiltered());
        }
    }
}

方法2

for (Product filterProduct: filteredPL) {
    for (Product masterProduct: masterPL) {
        //Delete the old product having old values
        masterProduct.remove(filterProduct);
        //Add product with new values
        masterProduct.add(filterProduct);
    }
} 

这两种方法都具有 n square 复杂度。

还有其他更好的方法可以降低复杂性吗?

5 个答案:

答案 0 :(得分:1)

您可以将主要产品列表转换为具有常量时间访问方法的哈希集。

Set<Product> masterPLHS = new HashSet<Product>(masterPL); // O(n)

for (Product filterProduct: filteredPL) { //O(n)
        if (masterPLHS.contains(filterProduct)) {  //O(1)
            // do stuff
        }
    }
}

答案 1 :(得分:1)

创建代码:

 Hashmap<String, Boolean> merged =new Hashmap<>();

 for(Product product : masterList)
       Merged.put(product.getName(), product.isfiltered());

 for(Product product : filteredList)
       Merged.put(product.getName(), product.isfiltered());

这是组合列表的解决方案。这里,如果name在两个数组列表中,那么根据您的要求,它在hashmap中可以是唯一的,值为filteredlist

这里复杂性的一个主要因素是

O(n + n)= O(2n)= O(n)

由于

答案 2 :(得分:0)

使用第三个列表怎么样?使用第三个列表会阻止List<Object> listA; List<Object> listB; List<Object> listMerged = new List<>(); // init listMerged listMerged.addAll(listA); for(Object elt: listB){ if (!listA.contains(elt)) listMerged.add(elt); } 在搜索中包含添加的元素。

<?php
foreach ($_GET as $gkey => $gvalue) {

if (!is_array($gvalue)) {
    $_GET[$gkey] = stripslashes($gvalue);
}
}
?>
<div class="col-md-7 text-right sm-text-right">
<?php
if ($_COOKIE[userid] > 0) {
    echo widget('Bootstrap Theme - Header - Member                             Links','',$w[website_id],$w);

} else { ?>
    <ul class="mini-nav nomargin list-inline <?php if ($wa[custom_159] != 4) { ?>vpad<?php } ?>">
        <?php
        if ($wa[custom_148] == "1") { ?>
            <li>
                <div id="google_translate_element" style="width:127px;"></div>
                <script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
                <script type="text/javascript">
                    function googleTranslateElementInit() {
                        new google.translate.TranslateElement({pageLanguage: 'en', autoDisplay: false, layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL}, 'google_translate_element');
                    }
                </script>
            </li>
            <?php
        }
        echo menuArray("header_mini_nav",0,$w); ?>
    </ul>
    <?php
} ?>
<div class="clearfix"></div>
<?php
if (($wa[custom_159] != 0 || $wa[custom_159] == "") && $_SERVER['HTTP_HOST'] != "www.securemypayment.com") {

    if($wa[custom_159] == 4) { ?>
        [widget=Bootstrap Theme - Banner - 320_50]
        <?php
    } else {
        $featurename = '';

        if ($wa[custom_159] == 3 || $wa[custom_159] == 1 || $wa[custom_159] == 2) {
            $featurename = 'search_results';
        }
        if ($wa[custom_159] == 5) {
            $featurename = 'coupons';
        }
        if ($wa[custom_159] == 6) {
            $featurename = 'events';
        }
        if ($wa[custom_159] == 7) {
            $featurename = 'jobs';
        }
        if ($wa[custom_159] == 8) {
            $featurename = 'products';
        }
        if ($wa[custom_159] == 9) {
            $featurename = 'properties';
        }
        if ($wa[custom_159] == 10) {
            $featurename = 'classifieds';
        }
        if ($wa[custom_159] == 11) {
            $featurename = 'videos';
        }
        if ($wa[custom_159] == 12) {
            $featurename = 'blog';
        }
        if ($wa[custom_159] == 13) {
            $featurename = 'articles';
        }
        ?>
        <form action="/<?php echo $featurename; ?>" name="frm1" class="form-inline website-search">
            <?php
            if($wa[custom_159] == 3 || $wa[custom_159] == 2 || $wa[custom_159] == 5 || $wa[custom_159] == 6 || $wa[custom_159] == 7 || $wa[custom_159] == 8 || $wa[custom_159] == 9 || $wa[custom_159] == 10 || $wa[custom_159] == 11 || $wa[custom_159] == 12 || $wa[custom_159] == 13) { ?>
                <div class="input-group input-group-sm bmargin sm-autosuggest">
                    <span class="input-group-addon hidden-md"><i class="fa fa-search"></i></span>
                    <input type="text" placeholder="<?php if ($wa[custom_159] == 3 || $wa[custom_159] == 1 || $wa[custom_159] == 2) { echo $label[keyword_search_default]; } else { ?>Search by Keyword...<? } ?>" value="<? if ($_GET[q]!='') { echo htmlspecialchars($_GET[q]); } ?>" name="q" class=" <? if ($wa[custom_159] == 3 || $wa[custom_159] == 1 || $wa[custom_159] == 2) { ?>member<? } else if ($wa[custom_159] != 9) { echo $featurename; } else { ?>property<? } ?>_search form-control input-sm" autocomplete="off">
                </div>
                <?php
            }
            if ($wa[custom_159] == 3 || $wa[custom_159] == 1 || $wa[custom_159] == 10) { ?>
                <div class="input-group input-group-sm bmargin">
                    <span class="input-group-addon hidden-md"><i class="fa fa-location-arrow"></i></span>
                    <input type="text" placeholder="<?=$label[location_search_default]?>" value="<? if ($_GET[location_value]!="") { echo htmlspecialchars($_GET[location_value]); } else if ($w[geocode_visitor_default]==1 && $w[geocode]==1 && $_SESSION[vdisplay]!="") { echo htmlspecialchars($_SESSION[vdisplay]); } ?>" id="location_google_maps_header" name="location_value" class="googleSuggest googleLocation form-control">
                </div>
                <?php
            } ?>
            <input type="submit" value="Search" class="btn btn-sm btn_search bmargin xs-btn-block">
        </form>
        <?php
    }
} ?>
</div>

答案 3 :(得分:0)

试试这个

Set<Product> masterPLHS = new HashSet<Product>(masterPL);

for (Product filterProduct: filteredPL) { 
      masterPLHS.add(filterProduct);
}

答案 4 :(得分:0)

这也应该有效:

List<Product> merged = filteredProductList.stream().filter(masterProductList::contains).collect(Collectors.toList());

编辑:但由于contains方法

,我认为它是n ^ 2