Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through set and continue [closed]

Tags:

java

iterator

I iterate through a Set. If a value of a CarDetail is not set i want that the loop starts again with the next CarDetail. But somehow my continue wont work. Isnt it possible to use a continue with an iterator?

final Set<CarDetail> tmpDetail = new HashSet<CarDetail>(details);
   for(Iterator<CarDetail> iter = tmpDetail.iterator(); iter.hasNext();){                       
      CarDetail detail = iter.next();
      if(detail.getBack() == null){
      continue;
   }
   ... do something
}
like image 416
krackmoe Avatar asked Jan 27 '26 10:01

krackmoe


1 Answers

There is nothing wrong with using continue with iterators. You can even use this with an enhanced for loop (for each loop):

final Set<CarDetail> tmpDetail = new HashSet<CarDetail>(details);
for(CarDetail detail : tmpDetail) {                           
      if(detail.getBack() == null) {
         System.out.println("Skipping over " + detail.toString() );
         continue;
      }
      System.out.println("Processing car detail: " + detail.toString() );
     //... do something
}

How are you sure that the continue statement is being ignored in the code you posted? With a few little println statements, we can verify that the continue is working as we expect.

like image 72
Thorn Avatar answered Jan 30 '26 02:01

Thorn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!