• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Updating ArrayList<Object> and removing duplicates

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working with Apachi POI. During the main code I collect the stockCode and the quantities. At this part of the code it writes everything to my worksheet. How do I updated the quantity of the stockCodes exp. 0032029507 should have a quantity of (2). Also after I update the quantity, I want to remove the duplicates.

Any suggestions.



  • Worksheet Item List
    1 0000015643
    1 0032029507
    4 0000005670
    16 0000005685
    2 0000003796
    30 0000000426
    6 0000000171
    3 0000011909
    2 0000015789
    5 0000004375
    2 000124-425
    3 0032103543
    2 0000014861
    2 0000000359
    4 0000012908
    3 0000016149
    5 0000000362
    1 0000015623
    2 0000000151
    4 0000000450
    1 0000015644
    1 0032029507
    4 0000005670
    16 0000005685
    2 0000003796
    30 0000000426
    6 0000000171
    3 0000011909
    2 0000015789
    5 0000004375
    2 000124-425
    3 0032103543
    2 0000014861
    2 0000000359
    4 0000012908
    3 0000016149
    5 0000000362
    1 0000015623
    2 0000000151
    4 0000000450
  •  
    Anthony Johnson
    Ranch Hand
    Posts: 45
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also I had that should be j++. I was working on another way and removed it.
     
    Ranch Hand
    Posts: 543
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    create a method that checks for duplicates(implement a find method and return null or -1 if the object does not exist)

    or you could just use a HashMap or even HashSet instead
     
    Anthony Johnson
    Ranch Hand
    Posts: 45
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I figured it out. See below.

     
    Marshal
    Posts: 6639
    177
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Anthony, thanks for posting your solution.  I've got a couple of questions and suggestions for you.

    * See comment below

    -------------------------------------------------------------

    * This can be simplified to

    ----------------------------------------------------------------
    * See comment below


     
    Anthony Johnson
    Ranch Hand
    Posts: 45
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Knute Snortum wrote:Anthony, thanks for posting your solution.  I've got a couple of questions and suggestions for you.

    * See comment below

    -------------------------------------------------------------

    * This can be simplified to

    ----------------------------------------------------------------
    * See comment below




    For your suggestion I really appreciate it. In a lot of my old comments, I announce that I'm getting back to using Java, or coding in general. That is a trick that I forgot about.

    For question two, yes there can be multiple. Which way you think would be faster.

    As for your first question, I'm not sure, I will try my best to run it through debug and let you know which I get a chance to work on it.
     
    Knute Snortum
    Marshal
    Posts: 6639
    177
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Anthony Johnson wrote:For question two, yes there can be multiple. Which way you think would be faster.


    It's not a matter of faster, it's a matter of completeness.  With the break you could miss duplicates.  Simply remove the break.

    As for your first question, I'm not sure, I will try my best to run it through debug and let you know which I get a chance to work on it.


    I'm pretty sure you will get an ArrayIndexOutOfBoundsException.
     
    It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
    Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
    https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!