8 Replies Latest reply: Sep 28, 2012 6:31 AM by notivago RSS

    Compare the List values

    800839
      Hi,


      I have 3 customerNames which is embedded in a customerList['Bob', 'Bob', 'Chandler'], among which 2 does have the name Bob. for which i need to compare the lists customerName with Bob and will set the value. Else I need to find the record which is not equal to Bob and set the value Hence i am using 2 for loops to conpare it. The problem here the matchedCustomerList is coming as empty (size=0) in the else condition. Hence I am unable to find
      the record which is not Bob(as it can come in any order)? Below is the code:-

      List<String> matchedCustomerList = null;
      if (customerId.equalsIgnoreCase("100")) {
      if (!CollectionUtils.isEmpty(customerList)) {
      for (CustomerVO customerVO : customerList) { *// all 3 records*
      matchedCustomerList = new ArrayList<String>();
      if ("Bob". equalsIgnoreCase(customerVO.getCustomerName())) {
      customerVO.setCustomerName(customerVO.getCustomerName());
      matchedCustomerList.add(customerVO.getCustomerName());// will contain only the matched customer name
      break;
      }
      }
      }
      } else { // if the customerName is not Bob
      for (CustomerVO customerVO: customerList) { *// all 3 records*
      for (String customerName : matchedCustomerList) { *// 2 matched records*
      if (!customerName.equalsIgnoreCase(customerVO.getCustomerName())) {
      customerVO.setCustomerName(customerVO.getCustomerName());
      break;
      }
      }
      }
      }
      }



      Please point me where I am going wrong.

      Thanks.
        • 1. Re: Compare the List values
          aksarben
          First, use code tags when posting code in this forum. Very few people will bother reading unformatted, messy code like this.

          Second, you're re-inventing the wheel. Read up on the Collections framework & use the tested, debugged code that already exists (for example,List.contains()). Among other things, this should reduce your code complexity by removing some loops.
          • 2. Re: Compare the List values
            800839
            Definitely i will do sir, this is just for testing pls do let know where I am going wrong?
            • 3. Re: Compare the List values
              doremifasollatido
              You initialize matchedCustomerList for every object in customerList. It should be initialized once, before the loop.

              What's the point of the following?
              customerVO.setCustomerName(customerVO.getCustomerName());
              That line just sets the name to the existing name.
              • 4. Re: Compare the List values
                800839
                Thanks, when i put the matchedCustomerList outside the loop , still it is not working. In am getting the list as empty in the else part?
                • 5. Re: Compare the List values
                  800268
                  If you mean matchedCustomerList if empty in the else, then that's pretty obvious:
                  List x = new List
                  if(condition) {
                    // fill list
                  }
                  else {
                    // list is always empty here
                  }
                  If that is not what you mean, post your current code (with code tags) and be specific which list is empty.

                  Aside since you want to compare case insensitive you could use contains on a new TreeSet<String>(String.CASE_INSENSITIVE_ORDER) for you matched names.
                  • 6. Re: Compare the List values
                    MrBabakishiyev
                    http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/
                    • 7. Re: Compare the List values
                      800839
                      Thanks Walter. What i ant is the in my else part I want to get the matchedCustomerList customername available

                      List x = new List
                      if(condition) {
                      // fill list
                      x.add(custVO.getName());
                      }
                      else {
                      // list is always empty here
                      for(String custName: x){ // here the list X is empty

                      }

                      }


                      How i can get the list value set in the if(condition) to the ELSE part? Please clarify.
                      • 8. Re: Compare the List values
                        notivago
                        I will give you some pointers on where you are going wrong:

                        1- Your post is messy, if you want help, do some honest effort to get your answers instead of just throwing code at people. In real life programing, this kind of coding attitude will bring you no love.

                        2 - Your code is messy, there is no clarity on what you code should do, it is ill formatted, full of nested decision points, you should strive to make your code speak for itself. For example:
                        if (customerId.equalsIgnoreCase("100")) {
                         ...
                        } else { // if the customerName is not Bob
                         ... 
                        } 
                        should be
                        if (customerId.equalsIgnoreCase("100") && customerName.equalsIgnoreCase("bob")) {
                         ...
                        } else { 
                         ... 
                        } 
                        Or
                        if (customerId.equalsIgnoreCase("100") ) {
                            if( !customerName.equalsIgnoreCase("bob") ) {
                                  ... do something if some one with bobs ID does not have bob name...
                            }
                         ...
                        } else { 
                         ... 
                        } 
                        This clearly states that you are trying to handle bob differently, besides, customer ID 100 being bob holds the implicit assumption that bob is customer ID 100, which you are not asserting at any point. If you want to be sure that you are handling a particular bob you should have something like:
                         public class CustomerVO implements Serializable, Comparable<CustomerVO> {
                           ...
                             public boolean is( Long id ) {
                                  return this.id.equals(id);
                             }
                          ...
                         }
                        
                        and
                        
                        if (customerBob.is(customerId)) {
                         ...
                        } else { 
                         ... 
                        } 
                        Now the code is telling you are searching for some proper bob and guaranties that you got the right bob for you money.

                        You are also not doing anything if the collection is empty, either because you explicitly test it and skip code when it is empty or when it is not bob implicitly as the for will never execute. It would be clearer in the code if the collection is empty for you to just stop at it and use the collection api for that, at the beginning of the mess:
                        if(customerList == null || customerList.isEmpty() ) {
                            return;
                        }
                        And this would put the code a few miles closer to be something comprehensible although there is much to work on it for it even work.

                        My advice to you: If you cant clearly state what you should accomplish with the code in English or whatever is you native language, you code will come out messy.
                        May the code be with you.