1

I have a list having field is and is_searchable data has

field_id i_searchable
1234     1
2344     0
4553     1

i am looping over the list to check if data is searchable then return true else false

 public boolean validate() {
        LOGGER.info("in validate");
        boolean isSearchValue = false;
        LOGGER.info("is isSearchable" + fieldList.size());
        for (Field formFieldValMap : fieldList) {
            if (formFieldValMap.getIsSearchableField().equals("0")) {

                isSearchValue = false;
            } else {
                isSearchValue = true;
            }
        }

        return isSearchValue;
    }

the above function is returning me true,it should me return me false.If the list has all the data in is_searchable has 1 then only it should return true.

pars
  • 153
  • 2
  • 4
  • 11
  • 1
    Just a tip: Use `"0".equals(formFieldValMap.getIsSearchableField())` to prevent ocasional NullPointerException – Renato Lochetti Oct 02 '12 at 16:16
  • 1
    @RenatoLochetti Unless you know the values in the list aren't null. See [Yoda Conditions](http://wiert.me/2010/05/25/yoda-conditions-from-stackoverflow-new-programming-jargon-you-coined/). – Tim Pote Oct 02 '12 at 16:17

4 Answers4

4

May be the other way is assume list has all 1's. Set it to false as soon as you see 0 and return from the loop.

  boolean isSearchValue = true;


    for (Field formFieldValMap : fieldList) {
                if (formFieldValMap.getIsSearchableField().equals("0")) {

                    isSearchValue = false;
                    return;
                } 
            }

NOTE: You may need to take care of other conditions like what do to if list empty etc.,

kosa
  • 65,990
  • 13
  • 130
  • 167
  • Depending on the size of the list, it may be worthwhile to just return false from inside the loop. The value of the answer as it stands, however, is that you have a single exit point. – Tim Pote Oct 02 '12 at 16:15
  • @TimPote: I was updating answer as you type in the comment. – kosa Oct 02 '12 at 16:17
  • 1
    Since his method is returning `isSearchValue` you probably should too. Or, simpler, just `return false`. – Tim Pote Oct 02 '12 at 16:20
1

If you want to complete the loop:

boolean isSearchValue = true;
for (Field formFieldValMap : fieldList) {
   isSearchValue &= formFieldValMap.getIsSearchableField().equals("1");
}
return isSearchValue;

Otherwise:

for (Field formFieldValMap : fieldList) {
   if (formFieldValMap.getIsSearchableField().equals("0")) {
       return false;
   }
}
return true;
David Grant
  • 13,929
  • 3
  • 57
  • 63
  • The merit of this one is that it is possible it will save you from a [Branch Prediction Fail](http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array). That's never really certain until you test it though. – Tim Pote Oct 02 '12 at 16:19
0

Your code is just telling whether the last element not equals "0". Use this:

 public boolean validate() {
        LOGGER.info("in validate");
        boolean isSearchValue = false;
        LOGGER.info("is isSearchable" + fieldList.size());
        for (Field formFieldValMap : fieldList) {
            if (formFieldValMap.getIsSearchableField().equals("0")) {

                return false; //if any
            } 
        }

        return true;
    } 
Marcus
  • 6,701
  • 4
  • 19
  • 28
0

I'm a little late to the party but for future readers who are using Java 8 this can be solved using the streams API.

public boolean validate() {
    return fieldList.stream().map(Field::getIsSearchableField).allMatch("1"::equals);
}
Indrek Ots
  • 3,701
  • 1
  • 19
  • 24