• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Mister PKI

SSL Certificates * SSL Tools * Certificate Decoder

  • Buy SSL Certificates
  • Blog
  • OpenSSL
  • Keytool
  • SSL Tools
  • Donate

Java for loop

February 10, 2021 by Mister PKI Leave a Comment

Learn how to program with the java for loop to iterate over Collections of Lists, Streams, and Arrays and the syntax for each version of the for loop.

For the complete list of java code examples, visit our GitHub Repo.

Java for loop examples

For each example below, assume an input of the values “1”, “2”, “3”, “4”, “5”.

Java for loop array

public static void forLoop(final String[] items) {
  for (int i = 0; i < items.length; i++) {
    System.out.println(items[i]);
  }
}
Output will render
1
2
3
4
5

This example is the classic for loop. While not the typical approach in later versions of Java, it remains the classic example of how to iterate a Collection with an index. The syntax is for (int i = 0; i < items.length; i++), where i is the index, items.length is some collection size or length, and i++ is the counter.

Java for loop list

public static void forLoop(final List<String> items) {
  for (int i = 0; i < items.size(); i++) {
    System.out.println(items.get(i));
  }
}
Output will render
1
2
3
4
5

Another example of the classic for loop, this example iterates over a Java List with items of type String. The syntax is the same.

Java for loop break

public static void forLoopBreak(final String[] items, final String match) {
  for (int i = 0; i < items.length; i++) {
    System.out.println(items[i]);
    if (items[i].equals(match)) {
      break;
    }
  }
}
Output will render
1
2
3

For loop exit by using the break keyword. If given the match value of 3, the loop will exit and stop processing after matching the value 3.

Java for loop continue

public static void forLoopContinue(final String[] items, final String match) {
  for (int i = 0; i < items.length; i++) {
    if (items[i].equals(match)) {
      continue;
    }
    System.out.println(items[i]);
  }
}
Output will render
1
2
4
5

To skip or continue in a for loop, use the continue keyword. If given the match value of 3, the processing will continue at the beginning of the loop and not compute the remaining lines of code in the loop when the match condition is true.

Java for each array

public static void forEach(final String[] items) {
  for (String item: items) {
    System.out.println(item);
  }
}
Output will render
1
2
3
4
5

The java foreach loop has mostly replaced the classic for loop. Any time an index is warranted, it makes sense to use the classic loop. If an index is not necessary, use the foreach loop. They syntax is for (String item: items) when takes each item out of the collection or array to then process.

Java for each list

public static void forEach(final List<String> items) {
  for (String item: items) {
    System.out.println(item);
  }
}
Output will render
1
2
3
4
5

The syntax for the foreach loop on a list is the same as on an array.

Java for each stream

public static void streamForEach(final List<String> items) {
  items.forEach(System.out::println);
}
Output will render
1
2
3
4
5

Java streams have given us many convenience methods for iterating over collections. In this case, a simple iteration would be to call items.stream.forEach, or items.forEach shorthand, and then process each item.

Visit our GitHub Repository for the code examples.

Let us know in the comments if you have any questions or would like to see additional examples.

Read more of our content.

Uncategorized

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Popular Posts

PKCS12

openssl s_client

Keytool

Keytool list

ECDSA vs RSA

OpenSSL

PKCS7

Certificate Decoder

Training Courses

Top online courses in IT & Software

Cyber Security Training

Udemy - The Complete Internet Security Privacy Course icon

Buy SSL Certificates

The SSL Store

Comodo Store

Sectigo Store

RapidSSL

Recent Posts

  • curl authentication – Certificate, Bearer token, and Basic Auth
  • Docker container keeps restarting
  • Java Invalid Signature File
  • openssl view certificate
  • Convert PEM to other formats

Footer

  • Twitter
  • YouTube

Pages

  • About Mister PKI
  • Blog
  • Compare and Buy Affordable PKI Certificates
  • Contact Us
  • Full Disclosure
  • Privacy Policy
  • SSL Tools – Certificate Decoder and Certificate Checker

Copyright © 2022