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

Mister PKI

SSL Certificates * SSL Tools * Certificate Decoder

  • PKI Training
  • OpenSSL
  • Keytool
  • PKI Tools
    • Apereo CAS
    • SAML
    • OpenID Connect
  • Get expert help

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

PKI for DevOps Engineers – Free Training Series

PKI for DevOps Training Hub

Lesson 1 – How TLS and PKI Work

Lesson 2 – Understanding X.509 Certificates

Lesson 3 – Certificate Chains Explained

Lesson 4 – Debug TLS with OpenSSL

Lesson 5 – Verify Certificate Chains

Lesson 6 – Creating CSRs with OpenSSL

Lesson 7 – Working with PKCS12 Certificates

Lesson 8 – Java Keystores and keytool

Lesson 9 – Certificate Expiration Monitoring

Lesson 10 – Automating Certificate Renewal

Lesson 11 – Common TLS Errors

Lesson 12 – PKI Architecture for DevOps

Recent Posts

  • Apache mod_auth_cas: Protect Web Applications with Apereo CAS and Docker
  • Apache mod_auth_mellon: Configure SAML SSO & Docker
  • Configure OpenID Connect (OIDC) in Apereo CAS 7.3 and 8
  • Certificate Ownership Is an Operating Model Problem, Not a Tooling Problem
  • OpenSSL dgst: Create Checksums and Verify Digital Signatures

Footer

  • Twitter
  • YouTube
Mister PKI

Practical security engineering for the systems that establish trust.

Learn OpenSSL Keytool Free training
Work together PKI consulting CAS consulting Sponsored contributions
Site About Support Mister PKI Cookie policy Search
© 2026 Mister PKI Secure systems. Clear guidance.