DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Trending

  • Solid Testing Strategies for Salesforce Releases
  • Docker Model Runner: Streamlining AI Deployment for Developers
  • Unlocking AI Coding Assistants Part 2: Generating Code
  • My LLM Journey as a Software Engineer Exploring a New Domain
  1. DZone
  2. Coding
  3. Java
  4. Simplifying Nested Loops With Java 8 Lambdas

Simplifying Nested Loops With Java 8 Lambdas

A great way to keep looping code out of your main logic in Java 8 apps.

By 
Michael Scharhag user avatar
Michael Scharhag
·
Apr. 12, 16 · Code Snippet
Likes (9)
Comment
Save
Tweet
Share
39.2K Views

Join the DZone community and get the full member experience.

Join For Free

This is just a quick tip for everyone who has to work with multi-dimensional arrays in Java 8 (or newer).

In this case, you might often end with code similar to this:

float[][] values = ...
for (int i = 0; i < values.length; i++) {
  for (int k = 0; k < values[i].length; k++) {
    float value = values[i][k];
    // do something with i, k and value
  }
}

If you are lucky, you can replace the loops with for-each loops. However, often the indices are required for computations inside the loop.

In such a case you can come up with a simple utility method that looks like this:

private void loop(float[][] values, BiConsumer<Integer, Integer> consumer) {
  for (int i = 0; i < values.length; i++) {
    for (int k = 0; k < values[i].length; k++) {
      consumer.accept(i, k);
    }
  }
}

We can now loop over array indices like this:

float[][] values = ...
loop(values, (i, k) -> {
  float value = values[i][k];
  // do something with i, k and value
});

This way you can keep the looping code out of your main logic.

Of course, you should change the shown loop() method so it fits your personal needs.

Java (programming language)

Published at DZone with permission of Michael Scharhag, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: