第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何合并 2 個(gè)對(duì)象列表,但是當(dāng)有 2 個(gè)對(duì)象重復(fù)時(shí),它會(huì)將兩者合并并更改數(shù)量?

如何合并 2 個(gè)對(duì)象列表,但是當(dāng)有 2 個(gè)對(duì)象重復(fù)時(shí),它會(huì)將兩者合并并更改數(shù)量?

白板的微信 2023-09-20 16:35:14
我有 2 個(gè)包含產(chǎn)品的訂單清單。我需要當(dāng)你合并列表時(shí),如果它包含完全相同的產(chǎn)品,請(qǐng)更改一個(gè)的數(shù)量并刪除另一個(gè)我嘗試了 2 個(gè)嵌套的 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 回答

?
墨色風(fēng)雨

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超6個(gè)贊

假設(shè)您想要將 lista2 產(chǎn)品合并到 lista1 并且您有 3 個(gè)產(chǎn)品。


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


查看完整回答
反對(duì) 回復(fù) 2023-09-20
?
冉冉說

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊

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


查看完整回答
反對(duì) 回復(fù) 2023-09-20
?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

這是一種使用nester for循環(huán)來處理它的方法,lista2將是組合列表,lista1將從其中刪除重復(fù)項(xiàng)(id設(shè)置為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);

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-09-20
  • 3 回答
  • 0 關(guān)注
  • 163 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)