0

Im having a hard time understanding what is happing with my logic. My goal is to add objects to an arraylist if a string on the object is not already present on current arraylist im making.

So to explain my code below/my intentions..

while I add to rowItems check for versionId that already has been added to rowItems and if so append the name of the current object.

Here is what I have so far:

      rowItems = new ArrayList<>();

            for (int i = 0; i < SearchResultsHolder.results.size(); i++) {
                SearchRowItem item = new SearchRowItem(SearchResultsHolder.results.get(i).get("LAST_NAME").toString() + ", " + SearchResultsHolder.results.get(i).get("FIRST_NAME").toString(),
                        SearchResultsHolder.results.get(i).get("BUSINESS_NAME").toString(),
                        SearchResultsHolder.results.get(i).get("LOB").toString(),
                        SearchResultsHolder.results.get(i).get("ID_NUMBER").toString(),
                        cleanCancelled(SearchResultsHolder.results.get(i).get("STATUS").toString()),
                        SearchResultsHolder.results.get(i).get("EFF_DATE").toString(),
                        SearchResultsHolder.results.get(i).get("EXP_DATE").toString(),
                        SearchResultsHolder.results.get(i).get("VERSION_ID").toString());

                if (i == 0){
                    rowItems.add(item);//add first object by default

                }

                else {
                    for (int p = 0; p < rowItems.size(); p++) {
                        // if the version id is equal to any of the others already in the rowItems array appent the name to the object already in the array
                        if (item.getVersionId().toString().equals(rowItems.get(p).getVersionId().toString()))
                        {
                            item.setName(item.getName().toString() + "\n" + rowItems.get(p).getName().toString());
                          break;
                        } else {
                            rowItems.add(item);

                        }
                    }
                }

            }
            System.out.println("row item" + rowItems.toString());

SearchResults holder has a layout like this:

[
    {
        VERSION_ID=50,
        STATUS=Active,
        ID_NUMBER=1234,
        FIRST_NAME=JOHN,
        LAST_NAME=DOE       
    },
        {
        VERSION_ID=50,
        STATUS=Active,
        ID_NUMBER=1234,
        FIRST_NAME=JANE,
        LAST_NAME=DOE       
    },
        {
        VERSION_ID=100,
        STATUS=Dead,
        ID_NUMBER=1234,
        FIRST_NAME=JOHN,
        LAST_NAME=DOE       
    },
    {
        VERSION_ID=100,
        STATUS=Dead,
        ID_NUMBER=1234,
        FIRST_NAME=JANE,
        LAST_NAME=DOE       
    },
]

And what Im trying to achieve with the objects in my class: (rowitems arraylist)

[
    {
        VERSION_ID=50,
        STATUS=Active,
        ID_NUMBER=1234,
        NAME=JANE,DOE
             JOHN,DOE      
    },
        {
        VERSION_ID=100,
        STATUS=Dead,
        ID_NUMBER=1234,
        NAME=JANE,DOE
             JOHN,DOE
    },

]

What I get :

[
    {
        VERSION_ID=50,
        STATUS=Dead,
        ID_NUMBER=1234,
        NAME=JOHN,DOE



    },
        {
        VERSION_ID=100,
        STATUS=Active,
        ID_NUMBER=1234,
        NAME=JOHN,DOE

    }


]
3
  • And what actually happens when you run it? Commented Feb 9, 2015 at 21:23
  • @DavidConrad Added what I get from it Commented Feb 9, 2015 at 21:32
  • Did you try stepping through with a debugger? Commented Feb 9, 2015 at 21:36

1 Answer 1

1

You have incorrect this condition.

if (item.getVersionId().toString().equals(rowItems.get(p).getVersionId().toString()))
                {
                    item.setName(item.getName().toString() + "\n" + rowItems.get(p).getName().toString());
                } else {
                    rowItems.add(item);

                }

It means now when not equals strings you add it to arraylist. It is not correct. You have to check if all values from rowItems are not equals to item string. Idea like this:

 boolean foundDup = false;
 for (int p = 0; p < rowItems.size(); p++) {

                // if the version id is equal to any of the others already in the rowItems array appent the name to the object already in the array
                if (item.getVersionId().toString().equals(rowItems.get(p).getVersionId().toString()))
                {
                    rowItems.get(p).setName(item.getName().toString() + "\n<ToSeeEndOfLine>" + rowItems.get(p).getName().toString()); // EDIT: this line is changed
                    foundDup = true;
                    break;
                } 
            }
     if (!foundDup)
            rowItems.add(item);
Sign up to request clarification or add additional context in comments.

7 Comments

To make it more efficient, you should also add a break after it has been found.
@kolisko I added the break and it did help get closer to what Im looking for but still not there, see new output with break added in for loop
What is the current output and current code you are using now?
Ah, I see you edited it. You your new code with "break" is still incorrect. Check my example, there is added the "foundDup" variable and the "if" condition outside the for-loop.
@kolisko Ok thats works a lot better! Still not getting the name appended on, will have to do some testing to see why. See my updated output
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.