亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何合并 2 個對象列表,但是當有 2 個對象重復時,它會將兩者合并并更改數量?

如何合并 2 個對象列表,但是當有 2 個對象重復時,它會將兩者合并并更改數量?

白板的微信 2023-09-20 16:35:14
我有 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);    }}
查看完整描述

3 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

假設您想要將 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);    

//Loop lista1

for (Product lista1Product:lista1){

  //Use iterator to easily remove items from list 2

  Iterator<Product> i = lista2.iterator();

  while (i.hasNext()){

    Product lista2Product = i.next();

    if (lista1Product.getIdProducto()!=null &&

       lista1Product.getIdProducto().equals(lista2Product.getIdProducto())){

       //Found same productID. Increase the quantity

       lista1Product.setCantidad(lista1Product.getCantidad()+lista2Product.getCantidad());

       //Remove from lista2

       i.remove();

     }

   }

 }

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}]


查看完整回答
反對 回復 2023-09-20
?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

public static void main(String[] args) {

    // TODO Auto-generated method stub


    List<Product> l1 = new ArrayList<Product>();

    l1.add(new Product("PEN", 10));

    l1.add(new Product("BALL", 505));

    l1.add(new Product("CAP", 88));

    List<Product> l2 = new ArrayList<Product>();

    l2.add(new Product("PEN", 9));

    l2.add(new Product("Apple", 10));


    int count =0;

    boolean found = false;

    for(int i=0;i<l2.size();i++) { // looping the second list

        count = 0;

        found = false;

        for(int j=0; j<l1.size(); j++) { // looping the first list and checking whether the 

            //same product code exist in the second list or not

            // if found the same product code then getting the quantity and adding the quantity

            // updating the new quantity value into first list

            //updating the flage value

            if(l1.get(j).getProductCode().equalsIgnoreCase(l2.get(i).getProductCode())) {

                count = l1.get(j).getQuantity() +l2.get(i).getQuantity();

                l1.get(j).setQuantity(count);

                found = true;

                break;

            }

        }

        if(!found) // if the product from second list not found in first list then this flag value  false.

            // then adding this product into first list

        {

            l1.add(l2.get(i));

        }


    }

    for(Product obj :l1) {

        System.out.println(obj.getProductCode() +" :" +obj.getQuantity());

    }


}

輸出:筆:19 球:505 帽:88 蘋果:10


查看完整回答
反對 回復 2023-09-20
?
慕萊塢森

TA貢獻1810條經驗 獲得超4個贊

這是一種使用nester for循環來處理它的方法,lista2將是組合列表,lista1將從其中刪除重復項(id設置為0)。


for (ProductoSolicitado product1: lista1) {

    boolean found = false;

    for (ProductoSolicitado product2: lista2) {

        if (product1.getIdProducto().equals(product2.getIdProducto())) {

            product2.setCantidad(product1.getCantidad() + product2.getCantidad());

            product1.setIdProducto("0");

            found = true;

            break;

        }

    }

    if(!found){

        lista2.add(product1);

    }

}


查看完整回答
反對 回復 2023-09-20
  • 3 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號