0

guys i am a beginner in java ... i want to make a function to loop through a hashmap for example it contains

0[3],3[4,5],6[2]

and to break when the method isSiteInherited is true else return false... here is what i made

  private boolean isInherited() {
  boolean isInherited = false;
    for (Entry<Integer, Set<Integer>> entry : siteIndeciesMap.entrySet()) {
      for (Integer index : entry.getValue()) {
          if(isSiteInherited(index)){
              break;
          }
      }
    }
    return false;
  }

if not sites are found inherited return false and if it enters the break it should break from all the method ... so what is wrong with this method

mm rr
  • 1
  • We'd need to see `isSiteInherited()`. – Steve Smith Jul 05 '17 at 13:39
  • 1
    "it should break from all the method" --> just `return false;` then? – Mena Jul 05 '17 at 13:39
  • You **break** loops. To leave the method, simply `return` from it. – GhostCat Jul 05 '17 at 13:44
  • @GhostCat I believe he had to `return true` and not break from his loops, so the duplicate will confuse him and other users won't it? – Turtle Jul 05 '17 at 13:47
  • When naming variables and other items, name parts should usually comprise natural-language words and as such should be spelled the same (in that part) as the natural-language word to avoid mistakes and confusion for maintainers. Thus `siteIndeciesMap` should be corrected. – Lew Bloch Jul 05 '17 at 13:53
  • @Nathan I added another duplicate ... – GhostCat Jul 05 '17 at 14:23

2 Answers2

0

Seems to be like you want to return instead of break. Without the use of labels, break only gets you out of one layer of loops anyway, and in this case, you need to report overall success, which isn't something your current approach does.

private boolean isInherited() {
    for (Entry<Integer, Set<Integer>> entry : siteIndeciesMap.entrySet()) {
        for (Integer index : entry.getValue()) {
            if (isSiteInherited(index)) {
                return true;
            }
        }
    }
    return false;
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • One most certainly can break out of more than one layer of loop at a time, contrary to what is posted here. – Lew Bloch Jul 05 '17 at 13:54
  • @LewBloch: If one uses labels, sure; however, I wasn't using labels. I feel my idiomatic approach is better; now that I'm not on mobile, I can clean it up a bit better. – Makoto Jul 05 '17 at 14:44
-1

You can either use a label to break from the outer loop (https://stackoverflow.com/a/886979/4949918).

The better way would probably be to use a boolean to say that you've broken from the inner loop in the outer loop.

  private boolean isInherited() {
  boolean isInherited = false;
  boolean shouldBreak = false;
    for (Entry<Integer, Set<Integer>> entry : siteIndeciesMap.entrySet()) {
      for (Integer index : entry.getValue()) {
          if(isSiteInherited(index)){
              shouldBreak = true;
              break;
          }
      }
      if (shouldBreak) {
           break;
      }
    }
    return false;
  }
Axnyff
  • 9,213
  • 4
  • 33
  • 37