Member-only story

Which is Faster? Performance in Java Spring Boot

Rishi
3 min read6 days ago

When developing applications in Java Spring Boot, performance can be a main issue. Developers need to take decision about which approach is faster and more efficient. So, we will compare different techniques used in Spring Boot to determine which performs better.

1. For Loop vs Stream API

For Loop:

import java.util.ArrayList;
import java.util.List;

public class ForLoopCheck {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < 1_000_000; i++) {
numbers.add(i);
}

long start = System.nanoTime();
int sum = 0;
for (int num : numbers) {
sum += num;
}
long end = System.nanoTime();

System.out.println("For Loop Execution Time: " + (end - start) + " ns");
}
}

Stream API:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

public class StreamCheck {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < 1_000_000; i++) {
numbers.add(i);
}

long start = System.nanoTime();
int sum =

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Rishi
Rishi

Written by Rishi

Tech professional specializing in Java development and caching logic with expertise in SaaS and automation. https://rishi-preethamm.blogspot.com

Responses (4)

Write a response