如何合并 2 个对象列表,但是当有 2 个对象重复时,它会将两者合并并更改数量?

我有 2 个包含产品的订单清单。


我需要当你合并列表时,如果它包含完全相同的产品,请更改一个的数量并删除另一个


我尝试了 2 个嵌套的 for,但似乎不起作用


for (ProductoSolicitado p: lista1) {

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

        if (p.getIdProducto().equals(lista1.get(i).getIdProducto())) {

            p.setCantidad(p.getCantidad() + lista1.get(i).getCantidad());

            lista1.get(i).setIdProducto("0");

            lista2.add(p);

            lista2.add(lista1.get(i));

        }

    }

}


for (ProductoSolicitado p: lista2) {

    if (!p.getIdProducto().equals("0")) {

        lista3.add(p);

    }

}


白板的微信
浏览 95回答 3
3回答

墨色风雨

假设您想要将 lista2 产品合并到 lista1 并且您有 3 个产品。P1 - P3 在 lista1 中,P2 - P3 在 lista2 中List<Product> lista1 = new ArrayList<>();List<Product> lista2 = new ArrayList<>();Product p1 = new Product();p1.setCantidad(1);p1.setIdProducto("1");Product p2 = new Product();p2.setCantidad(1);p2.setIdProducto("2");Product p3 = new Product();p3.setCantidad(1);p3.setIdProducto("3");lista1.add(p1);lista1.add(p3);lista2.add(p2);lista2.add(p3);&nbsp; &nbsp;&nbsp;//Loop lista1for (Product lista1Product:lista1){&nbsp; //Use iterator to easily remove items from list 2&nbsp; Iterator<Product> i = lista2.iterator();&nbsp; while (i.hasNext()){&nbsp; &nbsp; Product lista2Product = i.next();&nbsp; &nbsp; if (lista1Product.getIdProducto()!=null &&&nbsp; &nbsp; &nbsp; &nbsp;lista1Product.getIdProducto().equals(lista2Product.getIdProducto())){&nbsp; &nbsp; &nbsp; &nbsp;//Found same productID. Increase the quantity&nbsp; &nbsp; &nbsp; &nbsp;lista1Product.setCantidad(lista1Product.getCantidad()+lista2Product.getCantidad());&nbsp; &nbsp; &nbsp; &nbsp;//Remove from lista2&nbsp; &nbsp; &nbsp; &nbsp;i.remove();&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp;}System.out.println("Lista1: " + lista1.toString());System.out.println("Lista2: " + lista2.toString());输出是:Lista1: [Product{IdProducto='1', cantidad=1}, Product{IdProducto='3', cantidad=2}]Lista2: [Product{IdProducto='2', cantidad=1}]

冉冉说

public static void main(String[] args) {&nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; List<Product> l1 = new ArrayList<Product>();&nbsp; &nbsp; l1.add(new Product("PEN", 10));&nbsp; &nbsp; l1.add(new Product("BALL", 505));&nbsp; &nbsp; l1.add(new Product("CAP", 88));&nbsp; &nbsp; List<Product> l2 = new ArrayList<Product>();&nbsp; &nbsp; l2.add(new Product("PEN", 9));&nbsp; &nbsp; l2.add(new Product("Apple", 10));&nbsp; &nbsp; int count =0;&nbsp; &nbsp; boolean found = false;&nbsp; &nbsp; for(int i=0;i<l2.size();i++) { // looping the second list&nbsp; &nbsp; &nbsp; &nbsp; count = 0;&nbsp; &nbsp; &nbsp; &nbsp; found = false;&nbsp; &nbsp; &nbsp; &nbsp; for(int j=0; j<l1.size(); j++) { // looping the first list and checking whether the&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //same product code exist in the second list or not&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if found the same product code then getting the quantity and adding the quantity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // updating the new quantity value into first list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //updating the flage value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(l1.get(j).getProductCode().equalsIgnoreCase(l2.get(i).getProductCode())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = l1.get(j).getQuantity() +l2.get(i).getQuantity();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l1.get(j).setQuantity(count);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; found = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(!found) // if the product from second list not found in first list then this flag value&nbsp; false.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // then adding this product into first list&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l1.add(l2.get(i));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; for(Product obj :l1) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(obj.getProductCode() +" :" +obj.getQuantity());&nbsp; &nbsp; }}输出:笔:19 球:505 帽:88 苹果:10

慕莱坞森

这是一种使用nester for循环来处理它的方法,lista2将是组合列表,lista1将从其中删除重复项(id设置为0)。for (ProductoSolicitado product1: lista1) {&nbsp; &nbsp; boolean found = false;&nbsp; &nbsp; for (ProductoSolicitado product2: lista2) {&nbsp; &nbsp; &nbsp; &nbsp; if (product1.getIdProducto().equals(product2.getIdProducto())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; product2.setCantidad(product1.getCantidad() + product2.getCantidad());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; product1.setIdProducto("0");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; found = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if(!found){&nbsp; &nbsp; &nbsp; &nbsp; lista2.add(product1);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java