Link to home
Start Free TrialLog in
Avatar of chaitu chaitu
chaitu chaituFlag for India

asked on

arraylist contains method not working properly

in the chargelist i have got charges of 162,666,888

      for(int i=0;i<charge.length;i++)
            {
                  if(charge[i]==null || charge[i].trim().length()==0)
                        continue;
                  houseDocumentCharges                                                            = new HouseDocumentCharges();
                  houseDocumentCharges.costIncurredAt                        = StringUtility.getDefault(costIncurredAt[i],"D");
                  houseDocumentCharges.chargeId                                    = StringUtility.noNull(charge[i]);
                  houseDocumentCharges.chargeDescription                  = StringUtility.noNull(chargeDescription[i]);
                  chargesList.add(houseDocumentCharges);
            }

if the chargeId's having costIncurredAt is 'O' i  dont want to enter in to the if loop


666 chargeid is having  costIncurredAt is 'O'  still enter into the if loop ;

if 888 chargeid has costIncurredAt is 'D' should enter into if loop;i have written below but it didnt solve my purpose.

        if(chargeList!=null && chargeList.size()>0)
            {
    if (!chargeList.contains( new HouseDocumentCharges("620","O")) )
    {
                        userInputModel = new UserInputModel(validMap,"vendorCode",      ValidationConstants.MULTI_VENDOR_VALIDATION_NAME,5);//modified validation code from 2 to 5
                        validationModel.addUserInput("vendorId",userInputModel);
    }
    }


HouseDocumentCharges classs
****************************

      public HouseDocumentCharges(String chargeId,String costIncurredAt) {
       this.chargeId = chargeId;
        this.costIncurredAt = costIncurredAt;
   }

 public HouseDocumentCharges(String chargeId) {
       this.chargeId = chargeId;
        }
  public boolean equals(Object obj) {
       return chargeId.equals(((HouseDocumentCharges)obj).chargeId) || costIncurredAt.equals(((HouseDocumentCharges)obj).costIncurredAt);
   }
   public int hashCode() {
       return chargeId.hashCode();
   }
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You should probably have a

public String getCostIncurredAt() {
    return costIncurredAt;
}

method in that class and check against 'O'
Or rather against "O"
Avatar of chaitu chaitu

ASKER

HouseDocumentCharges classs WE dont set get methods only public variables defined.so in the controller we r assiging like this  houseDocumentCharges.costIncurredAt                    = StringUtility.getDefault(costIncurredAt[i],"D");
I don't quite understand when you do and don't want to enter the if block. Please try to explain more clearly
Shouldn't that be "&&" instead of "||" in your equals() method?
>> 666 chargeid is having  costIncurredAt is 'O'  still enter into the if loop ;
You mean this one?
>>  if (!chargeList.contains( new HouseDocumentCharges("620","O")) )  // Here's "620" not "666"
>> Please try to explain more clearly
I agree
in the chargesList i will get HousedocumentchargesObject

in this object say charges 162,666,888 and relevant desc and costIncurredAt will be stored


chargeid  costIncurredAt
162        "O"
666        "O"
888         "D"

if the charges having costIncurredAt "O" should not enter into this        if condition

  if(chargeList!=null && chargeList.size()>0)
          {
    if (!chargeList.contains( new HouseDocumentCharges("620","O")) )
    {

        userInputModel = new UserInputModel(validMap,"vendorCode",     ValidationConstants.MULTI_VENDOR_VALIDATION_NAME,5);//modified validation code from 2 to 5
                    validationModel.addUserInput("vendorId",userInputModel);

}
}
OTHER than chargeid 620  and costIncurredAt  "D" should enter into if condition
>> if the charges having costIncurredAt "O" should not enter into this        if condition
You mean that if one or more HouseDocumentCharge objects in that list have costIncurredAt = "O",
you shouldn't enter that if condition?


HouseDocumentCharges classs
*************************
   public HouseDocumentCharges(String costIncurredAt) {
        this.costIncurredAt = costIncurredAt;
   }

   public boolean equals(Object obj) {
       return costIncurredAt.equals(((HouseDocumentCharges)obj).costIncurredAt);
   }
   public int hashCode() {
       return costIncurredAt.hashCode();
   }

and then

   if (!chargeList.contains( new HouseDocumentCharges("O")) ) {
   }



yes ,i will get one or more HouseDocumentCharge objects in that list have costIncurredAt = "O" and chargeId's also;


other than 620 chargeId's  and costIncurredAt "D" should enter into if condition

can't i write like this

 if (!chargeList.contains( new HouseDocumentCharges("620","O")))
    {
..........
}
}


 public HouseDocumentCharges(String chargeId,String costIncurredAt) {
   
       this.chargeId = chargeId;
       this.costIncurredAt = costIncurredAt;
       
   }

         
       
  public boolean equals(Object obj) {
 
       return chargeId.equals(((HouseDocumentCharges)obj).chargeId) || costIncurredAt.equals(((HouseDocumentCharges)obj).costIncurredAt);
   }
   
   
????
I really don't understand :°(
So you want those two conditions combined?
YESSSSSSSSSSS
Because of the way you've defined equals, once *one* object is in there with a costIncurredAt of "O", no further ones will be added if you check with contains first (irrespective of chargeId)
>> other than 620 chargeId's  and costIncurredAt "D" should enter into if condition
Again,
then do you mean that
if your arraylist contains the HouseDocumentCharge having "620" as chargeId and "D" as costIncurredAt,
       you shouldn't get into that if part
else
       you should get into that if part

?
>> YESSSSSSSSSS
So,
1) if one or more HouseDocumentCharge objects in that list have costIncurredAt = "O",
    you shouldn't enter that if condition?
2) if your arraylist contains the HouseDocumentCharge having "620" as chargeId and "D" as costIncurredAt,
    you shouldn't get into that if part


1)
HouseDocumentCharge other than "620" as chargeId  and of costIncurredAt either "D" should enter into if condition

if(!HouseDocumentCharge.chargeid("620") && HouseDocumentCharge.chargeid("D") )
{

}

2)if HouseDocumentCharge having "620" as chargeId  irrespective of costIncurredAt either "O" or "D" should not enter into if condition
1)
HouseDocumentCharge other than "620" as chargeId  and of costIncurredAt "D" should enter into if condition

chaituu, is *this* the problem:

>>666 chargeid is having  costIncurredAt is 'O'  still enter into the if loop ;

? If so, that simply means there are none with "O" and none with "666" in the list already ...
So that means that you if condition is simply

       if ( chargeId is not "620" && costIncurredAt is "D" )
666 , 888 or 162 chargesid's are  having  costIncurredAt is 'O'  still enter into the if loop that is one of the problem;

another problem is if chargesid "620"  should not enter in if condition
yes
using contains method how can i write.can't i use  combination in Housedocumentcharges constructor
If I understand you well you don't want to iterate through your chargesList.
Well, then can't you determine if you're gonna need to ask for a vendorCode & ID at the moment of
populating the chargesList?

    boolean askForVendor = false;

     for(int i=0;i<charge.length;i++)
          {
               if(charge[i]==null || charge[i].trim().length()==0)
                    continue;
               houseDocumentCharges                                                  = new HouseDocumentCharges();
               houseDocumentCharges.costIncurredAt                    = StringUtility.getDefault(costIncurredAt[i],"D");
               houseDocumentCharges.chargeId                              = StringUtility.noNull(charge[i]);
               if (!askForVendor)
                  askForVendor = !chargeId.equals("620") && costInCurredAt.equals("D");
               houseDocumentCharges.chargeDescription               = StringUtility.noNull(chargeDescription[i]);
               chargesList.add(houseDocumentCharges);
          }

      if(chargeList!=null && chargeList.size()>0) {
         if ( askForVendor ) {
              userInputModel = new UserInputModel(validMap,"vendorCode",
                            ValidationConstants.MULTI_VENDOR_VALIDATION_NAME,5);//modified validation code from 2 to 5
              validationModel.addUserInput("vendorId",userInputModel);
        }
    }
You have skewed everything by the way you've implemented equals. You'd probably get on better if you were to have a Set,  or more, of exclusions:

Set exclusions = new HashSet();
exclusions.put("620");
// etc.

if (exclusions.contains(charge.chargeId )) {
   // do it (or not)
}


hi zzynx

i can't write that condition while adding to chargeList();at that time it has to be added..


I CAN  WRITE AS WELL like BELOW ONE

but my intension is without using iteration can't i do using some methods in ArraYLIST LIKE CONATINS AND INDEXof.

  if(chargeList!=null && chargeList.size()>0)
            {
   
      for(int i=0;i<chargeList.size();i++)
                  {
                        houseDocumentCharges = (HouseDocumentCharges)chargeList.get(i);
                  if(houseDocumentCharges.chargeId.equals("620") || houseDocumentCharges.costIncurredAt.equals("O")))
                        continue;

                        userInputModel = new UserInputModel(validMap,"vendorCode",      ValidationConstants.MULTI_VENDOR_VALIDATION_NAME,5);//modified validation code from 2 to 5
                        validationModel.addUserInput("vendorId",userInputModel);

                  }

    }
>>f(houseDocumentCharges.chargeId.equals("620") || houseDocumentCharges.costIncurredAt.equals("O")))
                    continue;

That's perfectly OK it seems to me
i.e. it is clear, readable and does what you want. There's no shorter way to implement it that i can see, since you say that you  can't alter the way the collection is filled
what do u want to alter the collection ?
No - i'm saying that since you have no control over how the collection chargesList is filled, it's perfectly OK to use the code you just posted
>>There's no shorter way to implement it that i can see,

Although your way is short, using an exclusion Set like i posted earlier will prevent large groups of if blocks building up and will be easier to maintain
>> i can't write that condition while adding to chargeList();
Can you explain why not?
>> you have no control over how the collection chargesList is filled
Is that correct?
while adding to chargeList() we have to add all the charges it will be used some where else;i can't filter there itself.

>> you have no control over how the collection chargesList is filled
not correct
>> while adding to chargeList() we have to add all the charges it will be used some where else;
>> i can't filter there itself.
That's not what my code does.
While adding **ALL** the HouseDocumentCharge objects to your list, we just keep one boolean up-to-date
according to the objects added.

Further on, we simply use that boolean to decide.
Please reread/try that code

below code written in one private method and put the arraylist in the session
for(int i=0;i<charge.length;i++)
          {
               if(charge[i]==null || charge[i].trim().length()==0)
                    continue;
               houseDocumentCharges                                                  = new HouseDocumentCharges();
               houseDocumentCharges.costIncurredAt                    = StringUtility.getDefault(costIncurredAt[i],"D");
               houseDocumentCharges.chargeId                              = StringUtility.noNull(charge[i]);
               if (!askForVendor)
                  askForVendor = !chargeId.equals("620") && costInCurredAt.equals("D");
               houseDocumentCharges.chargeDescription               = StringUtility.noNull(chargeDescription[i]);
               chargesList.add(houseDocumentCharges);
          }

both these codes are not in same method...in our application.

below code written in another private method

      if(chargeList!=null && chargeList.size()>0) {
         if ( askForVendor ) {
              userInputModel = new UserInputModel(validMap,"vendorCode",
                            ValidationConstants.MULTI_VENDOR_VALIDATION_NAME,5);//modified validation code from 2 to 5
              validationModel.addUserInput("vendorId",userInputModel);
        }


 
>> below code written in another private method
another private method **of the same class**?
If from the same class:

public class ThatSameClass {

     private boolean askForVendor = false;

     private void someMethod1() {
 
         ...
          for(int i=0;i<charge.length;i++)
          {
               if(charge[i]==null || charge[i].trim().length()==0)
                    continue;
               houseDocumentCharges                                                  = new HouseDocumentCharges();
               houseDocumentCharges.costIncurredAt                    = StringUtility.getDefault(costIncurredAt[i],"D");
               houseDocumentCharges.chargeId                              = StringUtility.noNull(charge[i]);
               if (!askForVendor)
                  askForVendor = !chargeId.equals("620") && costInCurredAt.equals("D");
               houseDocumentCharges.chargeDescription               = StringUtility.noNull(chargeDescription[i]);
               chargesList.add(houseDocumentCharges);
          }

         ...
     }


     private void someOtherMethod2() {
         ...
           if(chargeList!=null && chargeList.size()>0) {
               if ( askForVendor ) {
                    userInputModel = new UserInputModel(validMap,"vendorCode",
                             ValidationConstants.MULTI_VENDOR_VALIDATION_NAME,5);//modified validation code from 2 to 5
                   validationModel.addUserInput("vendorId",userInputModel);
              }
           }
         ...
     }

}
other than bove can't u do using contains or indexOf methods.

in HouseDocumentCharges class what i have to write?
chaituu, it would help if you try to say what you're trying to achieve rather than asking questions about specific lines of code or other detailed parts of code. Look at the following:

http://www.catb.org/~esr/faqs/smart-questions.html#goal
thank u for ur link;
one thing to say these are not my home work questions
>> one thing to say these are not my home work questions

I didn't think they were ;-)
Perhaps it would be helpful if we were told *why* the question's closure has been requested
because i didn't get proper solution for my question;

i solved my problem using below code which is accepted by u also in ur previous posts..


if(chargeList!=null && chargeList.size()>0)
          {
   
     for(int i=0;i<chargeList.size();i++)
               {
                    houseDocumentCharges = (HouseDocumentCharges)chargeList.get(i);
               if(houseDocumentCharges.chargeId.equals("620") || houseDocumentCharges.costIncurredAt.equals("O")))
                    continue;

                    userInputModel = new UserInputModel(validMap,"vendorCode",     ValidationConstants.MULTI_VENDOR_VALIDATION_NAME,5);//modified validation code from 2 to 5
                    validationModel.addUserInput("vendorId",userInputModel);

               }

    }
OK then. In future it would be better if you were to request deletion earlier, as, from memory, i seem to remember that the two questions were running concurrently for some time
" seem to remember that the two questions were running concurrently for some time"

THOSE two questions are little bit different from each other;if they r same i shouldn't have posted
I'm talking about *this* one and the other one

i think ur talking abt this link and the presnt one;
https://www.experts-exchange.com/questions/21411969/without-using-iteration-can-i-know-in-the-arraylist-of-Housedocumentcharges-Object-whether-620-chargeid-is-there-or-not-Is-there-any-method-is-there-in-arraylist-or-in-collections.html

in the above link In the constructor i am passing one argument in conatains method at that time its works well;

in the presnt post i am passing 2 arguments to contains method;;it didnt serve my purpose ;so i have implemented my own solutions;it works for me;so i decided to refund my points;

if u think that i want to delete this post i will ask the moderator as u wish
It's OK with me to delete and refund
ASKER CERTIFIED SOLUTION
Avatar of Lunchy
Lunchy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial