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

asked on

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?

i have arraylist of Housedocumentcharges Object;in this i have different chargid's and  chargesdescription will be stored.

 in the arraylist if the chargeid is 620 is there i dont want to enter into UserInputModel method;


I can solve the problem using below code.but without using for loop(or 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?

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

      userInputModel = new UserInputModel(vndrList,"vendorIdLabel", ValidationConstants.MULTI_VENDOR_VALIDATION_NAME,4);
      validationModel.addUserInput("Vendor(s)",userInputModel);
      }
}
Avatar of zzynx
zzynx
Flag of Belgium image

ArrayList has a method contains()
public boolean contains(Object elem)

Returns true if this list contains the specified element.

Parameters: elem - element whose presence in this List is to be tested.
Returns: true if the specified element is present; false otherwise.
if (chargesList1.contains("620"))
{
   ...
You can also use i=the indexOf() method to get the index

int index = chargesList1.indexOf("620");
if (index==-1)
{
   // not there
}
else
{
   // its in list
}
So,

       chargesList.contains(...)

but the parameter you pass must be the exact object you're looking for (equals() must be true)
>> but the parameter you pass must be the exact object you're looking for (equals() must be true)
So, a simple
     if (chargesList1.contains("620"))
won't work, since your arraylist doesn't contain Strings but Housedocumentcharges objects
Avatar of chaitu chaitu

ASKER



so contains and indexOf won't work here.........any other alternative
>> so contains and indexOf won't work here.
I didn't say that.

**If** your Housedocumentcharges class has equals() & hashCode() methods
that are based on the chargeId, then passing a new Housedocumentcharges object with chargeId "620" is sufficient:

if ( chargesList1.contains( new Housedocumentcharges("620") )
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium 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
we haven't  implemented these two methods in HouseDocumentCharges class and also the Housedocumentcharges constructor

  public boolean equals(Object obj) {
       return chargeId.equals( ((Housedocumentcharges)obj).getChargeId() );
   }
   public int hashCode() {
       return getChargeId().hashCode();
   }

>> we haven't  implemented these two methods
Well, if you want to use contains() you should ;°)
we  dont have get set methods in bean class only public variables defined like public String chargeId.so now i written below one in bean class

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

  public boolean equals(Object obj) {
       return chargeId.equals( ((HouseDocumentCharges)obj).chargeId );
   }
   public int hashCode() {
       return chargeId.hashCode();
   }
Thanks for accepting