• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

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);
      }
}
0
chaitu chaitu
Asked:
chaitu chaitu
  • 8
  • 3
  • 3
1 Solution
 
zzynxSoftware engineerCommented:
ArrayList has a method contains()
0
 
zzynxSoftware engineerCommented:
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.
0
 
objectsCommented:
if (chargesList1.contains("620"))
{
   ...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
objectsCommented:
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
}
0
 
zzynxSoftware engineerCommented:
So,

       chargesList.contains(...)

but the parameter you pass must be the exact object you're looking for (equals() must be true)
0
 
zzynxSoftware engineerCommented:
>> 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
0
 
chaitu chaituAuthor Commented:


so contains and indexOf won't work here.........any other alternative
0
 
zzynxSoftware engineerCommented:
>> 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") )
0
 
zzynxSoftware engineerCommented:
E.g.

public class Housedocumentcharges {

   private String chargeId;

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

   public String getSChargeId() {
      return chargeId;
   }

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

Then

  if ( chargesList1.contains( new Housedocumentcharges("620") )

should work
0
 
chaitu chaituAuthor Commented:
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();
   }

0
 
zzynxSoftware engineerCommented:
>> we haven't  implemented these two methods
Well, if you want to use contains() you should ;°)
0
 
chaitu chaituAuthor Commented:
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();
   }
0
 
zzynxSoftware engineerCommented:
Thanks for accepting
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now