2

Iam wondering about the best way to prevent accessing to multiple loops in a scalable way

String token;
ArrayList numbers;
ArrayList names;

for(String n : numbers){
   if(token.equals(n)){
      break;
   }
}

for(String n : names){
   if(token.equals(n)){
      break;
   }
}

I want to skip accessing the 2nd loop if i found a hit on the first one but i want to do it in a scalable way because maybe later i will have 5 lists

Solutions that i thought about

  1. Make multiple returns inside the loops, but this is not a good programming technique

  2. Set a flag and use it in the other loops(either by if condition or put it in the loop condition, but when we have 5 different lists, it means i will have 5 flags and checks on the loop before me.

I would appreciate your suggestions. Thank you

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Shady Hussein
  • 513
  • 8
  • 24
  • 4
    Returning from multiple places is absolutely fine, IMO. A lot of the reasons for *not* doing it are based on older programming languages where you'd want to perform cleanup etc on return. – Jon Skeet Feb 12 '13 at 09:48
  • I just googled the "one entry one exit". Yes you are right about that, it is just a legacy now and doesn't have any effect on performance. But on the other hand, i think if you have a big function it will be confusing if you have 10 returns instead of changing one variable and make one return at the end. At least it is easier for me to debug this variable and put it in the watch – Shady Hussein Feb 12 '13 at 10:09
  • How about creating a list from all your lists (the order of the lists is up to you), you will then have one list and one loop. You break when there's a match. – Hoa Feb 12 '13 at 10:16
  • @ShadyAziz: It's not a matter of *performance* - it's a matter of readability. I think it's *less* confusing if the code says exactly what you want to do: when you've found an item, you know you want to return... so make the code do *exactly* that. Why would you want to go the indirect route of setting a variable and continuing through a load of other code you *know* you don't care about? – Jon Skeet Feb 12 '13 at 10:19
  • [This StackOverflow answer](http://stackoverflow.com/a/36729/151344) might be a interesting discussion for you to read, regarding what was mentioned by Jon Skeet. – Alderath Feb 12 '13 at 10:20

3 Answers3

4
public static boolean contains (Object element, Collection <?> ... collections)
{
    for (Collection c: collections)
        if (c.contains (element)) return true;

    return false;
}
Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
  • This solution is perfect as long as you only want to check the existence of an object in an array. In my case it is alittle more complicated because objects are not the same. So iam comparing between their properties. I just used the above example to easily clarify my problem. but +1 for that :) – Shady Hussein Feb 12 '13 at 10:04
  • You can put whatever logic inside `for` loop in my example. – Mikhail Vladimirov Feb 12 '13 at 10:12
1

What about using a Set or a Guava MultiSet if your ArrayList has duplicates.

Then you just need to do

        String token = "";
        Set<String> numbers = Sets.newHashSet();
        Set<String> names = Sets.newHashSet();
        if (numbers.contains(token)) {

        }
        else if (names.contains(token)) {

        }

A check for contains(token) will almost always be faster on a Set than on a List.

anthonyms
  • 950
  • 1
  • 10
  • 20
0

Are you aware that you can tag loops, and pass the tag as a parameter to the break or continue statements?

Like so:

mytag: for(int i =0; i < 10; i++){
  for(int j =0; j < 10; j++){
    if(done){
      break mytag;
    }
  }
}

This works on switch, while and do-while, for continue and break.

Miquel
  • 15,405
  • 8
  • 54
  • 87
  • Yes iam aware, but what if i have 5 lists that require 5 loops. I will embed them inside each other. This is unscalable solution and will be confusing, thats what i think. – Shady Hussein Feb 12 '13 at 09:59
  • Ok, cool. sorry, maybe I should have put this as a comment, but I wanted to include the example. – Miquel Feb 12 '13 at 10:39