?
Solved

arraylist contains method not working properly

Posted on 2005-05-04
50
Medium Priority
?
260 Views
Last Modified: 2012-05-05
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();
   }
0
Comment
Question by:chaitu chaitu
  • 20
  • 16
  • 12
  • +1
49 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13925391
You should probably have a

public String getCostIncurredAt() {
    return costIncurredAt;
}

method in that class and check against 'O'
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13925392
Or rather against "O"
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13925400
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");
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 13925436
I don't quite understand when you do and don't want to enter the if block. Please try to explain more clearly
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13925443
Shouldn't that be "&&" instead of "||" in your equals() method?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13925449
>> 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"
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13925452
>> Please try to explain more clearly
I agree
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13925553
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);

}
}
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13925566
OTHER than chargeid 620  and costIncurredAt  "D" should enter into if condition
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13925595
>> 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")) ) {
   }



0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13925632
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);
   }
   
   
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13925641
????
I really don't understand :°(
So you want those two conditions combined?
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13925661
YESSSSSSSSSSS
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13925674
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)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13925678
>> 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

?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13925698
>> 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


0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13925724
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
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13925730
1)
HouseDocumentCharge other than "620" as chargeId  and of costIncurredAt "D" should enter into if condition

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13925752
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 ...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13925773
So that means that you if condition is simply

       if ( chargeId is not "620" && costIncurredAt is "D" )
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13925777
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
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13925784
yes
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13925792
using contains method how can i write.can't i use  combination in Housedocumentcharges constructor
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13925823
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);
        }
    }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13925829
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)
}


0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13925886
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);

                  }

    }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13925913
>>f(houseDocumentCharges.chargeId.equals("620") || houseDocumentCharges.costIncurredAt.equals("O")))
                    continue;

That's perfectly OK it seems to me
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13925939
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
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13925960
what do u want to alter the collection ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13925984
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13926123
>>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
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13926141
>> 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?
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13926573
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
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13926698
>> 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
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13926777

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);
        }


 
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13927004
>> 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);
              }
           }
         ...
     }

}
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13933096
other than bove can't u do using contains or indexOf methods.

in HouseDocumentCharges class what i have to write?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13933681
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
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13933694
thank u for ur link;
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13933695
one thing to say these are not my home work questions
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13933696
>> one thing to say these are not my home work questions

I didn't think they were ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13952026
Perhaps it would be helpful if we were told *why* the question's closure has been requested
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13957080
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);

               }

    }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13958114
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
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13958685
" 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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13958710
I'm talking about *this* one and the other one
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13965612

i think ur talking abt this link and the presnt one;
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21411969.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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13966621
It's OK with me to delete and refund
0
 
LVL 2

Accepted Solution

by:
Lunchy earned 0 total points
ID: 13988871
Closed, 250 points refunded.
Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question