如何将类的集合添加到具有集合的类中

发布于 2024-11-09 09:19:03 字数 3680 浏览 0 评论 0原文

我有一个产品数组列表,我想将其添加到我的产品类中的销售类地图中。这样做的目的是让每个产品记录所购买的商品以及购买金额。

ArrayList<Product> array = new ArrayList<Product>(tempProducts.values());
for (int i = 0; i < array.size() - 1; i++) {
            for (int j = i+1; j < array.size() ; j++) {
                update(array.get(i), array.get(j));
            }
        }
    }


 public static void update(Product a, Product b)
{



    if (a.getId().compareToIgnoreCase(b.getId()) != 0) {
        if (a.getCollection().get(b.getId()) != null) {
            a.getCollection().get(b.getId()).incsales();

        } else {

            a.getCollection().put(b.getId(), new sale(b.getId(), b.getDesc()));
        }
        if (b.getCollection().containsKey(a.getId())) {
            a.getCollection().get(b.getId()).incsales();

        } else {
            b.getCollection().put(a.getId(), new sale(a.getId(), a.getDesc()));
        }
    }



}

我得到的结果是:

 Description : Balsamic Ital Dressing 2.5L Kraft
 Sales : 80
 This product was purchased the most with :
 1 0000795501891 Babyfood, dinner, vegetables and lamb, junior (7)
 2 0000053838530 Cheese, cottage, creamed, large or small curd (7)
 3 0001580359521 Macadamia Nuts Kernels 1KG Natural Grocer (7)
 4 0001549470330 Orange Juice Long Life 2L Just Juice (7)
 5 0000102800862 Babyfood, fruit, pears and pineapple, junior (5)
 Description : Mighty Soft Bread Multigrain Sandwich 700g
 Sales : 78
 This product was purchased the most with :
 1 0000250315049 Babyfood, cereal, barley, prepared with whole milk (7)
 2 0001906925229 Coles Bread Multigrain Sliced 700g (7)
 3 0001348704022 Honey Portion 48X14G Beerenberg (7)
 4 0000965461817 Milk, canned, evaporated, nonfat, with added vitamin A and vitamin D (7)
  5 0000883156398 Abotts Village Bakery Wholemeal Farmhouse 750g (5)

我应该得到:

   Description : Balsamic Ital Dressing 2.5L Kraft 
  Sales : 80
  This product was purchased the most with :
  1 0000102800862 Babyfood, fruit, pears and pineapple, junior (4)
  2 0000449778467 Decaf Ground Coffee 250G Cremadoro (4)
  3 0001549470330 Orange Juice Long Life 2L Just Juice (4)
  4 0000795501891 Babyfood, dinner, vegetables and lamb, junior (3)
  5 0000708398761 Butter, salted (3)
  Description : Mighty Soft Bread Multigrain Sandwich 700g
  Sales : 78
  This product was purchased the most with :
  1 0000497527798 Cream Cheese Philadelphia Light 1KG Kraft (4)
  2 0000890198554 Mayonnaise Fat Free 2.7KG Kraft (4)
  3 0000298379350 Milk, buttermilk, dried (4)
  4 0000250315049 Babyfood, cereal, barley, prepared with whole milk (3)
  5 0000966839744 Babyfood, cereal, with egg yolks, strained (3)

关于如何阻止它过度计数的任何提示或线索?


更新: 尝试了 Péter Török 的建议,得到了这个结果:

 Description : Balsamic Ital Dressing 2.5L Kraft
    Sales : 80
    This product was purchased the most with :
    1 0001549470330 Orange Juice Long Life 2L Just Juice (7)
    2 0000102800862 Babyfood, fruit, pears and pineapple, junior (5)
    3 0000708398761 Butter, salted (5)
    4 0002140785264 Egg, whole, dried, stabilized, glucose reduced (5)
    5 0000422477496 Essence Parisian 575ML Aeroplane (5)
    Description : Mighty Soft Bread Multigrain Sandwich 700g
   Sales : 78
   This product was purchased the most with :
    1 0001906925229 Coles Bread Multigrain Sliced 700g (5)
    2 0000127034559 Limonata 24X200ML San Pellegrino (5)
    3 0001736609947 Babyfood, dessert, custard pudding, vanilla, junior (3)
    4 0002028785759 Babyfood, dinner, beef stew, toddler (3)
    5 0000432411254 Babyfood, juice, orange and pineapple (3)

I have an ArrayList of Products, that i want to add to My map of sales class in my Product class. The purpose of this is to have each product record what was bought with it and how much.

ArrayList<Product> array = new ArrayList<Product>(tempProducts.values());
for (int i = 0; i < array.size() - 1; i++) {
            for (int j = i+1; j < array.size() ; j++) {
                update(array.get(i), array.get(j));
            }
        }
    }


 public static void update(Product a, Product b)
{



    if (a.getId().compareToIgnoreCase(b.getId()) != 0) {
        if (a.getCollection().get(b.getId()) != null) {
            a.getCollection().get(b.getId()).incsales();

        } else {

            a.getCollection().put(b.getId(), new sale(b.getId(), b.getDesc()));
        }
        if (b.getCollection().containsKey(a.getId())) {
            a.getCollection().get(b.getId()).incsales();

        } else {
            b.getCollection().put(a.getId(), new sale(a.getId(), a.getDesc()));
        }
    }



}

The Results I am getting are :

 Description : Balsamic Ital Dressing 2.5L Kraft
 Sales : 80
 This product was purchased the most with :
 1 0000795501891 Babyfood, dinner, vegetables and lamb, junior (7)
 2 0000053838530 Cheese, cottage, creamed, large or small curd (7)
 3 0001580359521 Macadamia Nuts Kernels 1KG Natural Grocer (7)
 4 0001549470330 Orange Juice Long Life 2L Just Juice (7)
 5 0000102800862 Babyfood, fruit, pears and pineapple, junior (5)
 Description : Mighty Soft Bread Multigrain Sandwich 700g
 Sales : 78
 This product was purchased the most with :
 1 0000250315049 Babyfood, cereal, barley, prepared with whole milk (7)
 2 0001906925229 Coles Bread Multigrain Sliced 700g (7)
 3 0001348704022 Honey Portion 48X14G Beerenberg (7)
 4 0000965461817 Milk, canned, evaporated, nonfat, with added vitamin A and vitamin D (7)
  5 0000883156398 Abotts Village Bakery Wholemeal Farmhouse 750g (5)

i should be getting :

   Description : Balsamic Ital Dressing 2.5L Kraft 
  Sales : 80
  This product was purchased the most with :
  1 0000102800862 Babyfood, fruit, pears and pineapple, junior (4)
  2 0000449778467 Decaf Ground Coffee 250G Cremadoro (4)
  3 0001549470330 Orange Juice Long Life 2L Just Juice (4)
  4 0000795501891 Babyfood, dinner, vegetables and lamb, junior (3)
  5 0000708398761 Butter, salted (3)
  Description : Mighty Soft Bread Multigrain Sandwich 700g
  Sales : 78
  This product was purchased the most with :
  1 0000497527798 Cream Cheese Philadelphia Light 1KG Kraft (4)
  2 0000890198554 Mayonnaise Fat Free 2.7KG Kraft (4)
  3 0000298379350 Milk, buttermilk, dried (4)
  4 0000250315049 Babyfood, cereal, barley, prepared with whole milk (3)
  5 0000966839744 Babyfood, cereal, with egg yolks, strained (3)

Any hints or clues on how i can stop it from over counting ?


update :
Tried Péter Török Suggestion, Got this result:

 Description : Balsamic Ital Dressing 2.5L Kraft
    Sales : 80
    This product was purchased the most with :
    1 0001549470330 Orange Juice Long Life 2L Just Juice (7)
    2 0000102800862 Babyfood, fruit, pears and pineapple, junior (5)
    3 0000708398761 Butter, salted (5)
    4 0002140785264 Egg, whole, dried, stabilized, glucose reduced (5)
    5 0000422477496 Essence Parisian 575ML Aeroplane (5)
    Description : Mighty Soft Bread Multigrain Sandwich 700g
   Sales : 78
   This product was purchased the most with :
    1 0001906925229 Coles Bread Multigrain Sliced 700g (5)
    2 0000127034559 Limonata 24X200ML San Pellegrino (5)
    3 0001736609947 Babyfood, dessert, custard pudding, vanilla, junior (3)
    4 0002028785759 Babyfood, dinner, beef stew, toddler (3)
    5 0000432411254 Babyfood, juice, orange and pineapple (3)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏尔 2024-11-16 09:19:03

我认为问题的根源在于,对于索引为 (i,j) 的每一对产品,您在循环中将它们处理两次:一次作为 (i,j),然后作为 (j,i)。尝试使用此循环:(

for (int i = 0; i < array.size() - 1; i++) {
        for (int j = i + 1; j < array.size(); j++) {
            update(array.get(i), array.get(j));
        }
    }
}

这里内部循环计数器从 i + 1 而不是 0 开始。另请注意,我修改了两个循环条件,因为我认为您原来的循环有差一个错误 - 数组中的最后一个元素的索引为 array.size() - 1。)

更新

这里还有一个复制粘贴错误:

    if (b.getCollection().containsKey(a.getId())) {
        a.getCollection().get(b.getId()).incsales();

而它应该是

        b.getCollection().get(a.getId()).incsales();

I think the root of the problem is that for every pair of products with the index (i,j), you process them twice in your loop: once as (i,j), then as (j,i). Try with this loop instead:

for (int i = 0; i < array.size() - 1; i++) {
        for (int j = i + 1; j < array.size(); j++) {
            update(array.get(i), array.get(j));
        }
    }
}

(here the inner loop counter starts from i + 1 instead of 0. Note also that I modified both loop conditions because I think your original loops have an off by one error - the last element in the array has the index array.size() - 1.)

Update

And there is also a copy-paste bug here:

    if (b.getCollection().containsKey(a.getId())) {
        a.getCollection().get(b.getId()).incsales();

whereas it should be

        b.getCollection().get(a.getId()).incsales();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文