Range and Aggregate Operations
range, sum, average, summaryStatistics.
Range and Aggregate Operations is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Generating Ranges
IntStream.range(start, end) produces consecutive integers from start up to but not including end.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
IntStream.range(0, 5).forEach(System.out::println);
}
}rangeClosed Includes the End
IntStream.rangeClosed(start, end) includes the final value, so rangeClosed(1, 5) yields 1 through 5.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int sum = IntStream.rangeClosed(1, 5).sum();
System.out.println(sum);
}
}Replacing Index Loops
Ranges are a clean replacement for index-based for loops when iterating array positions.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
String[] names = {"a", "b", "c"};
IntStream.range(0, names.length)
.forEach(i -> System.out.println(i + ": " + names[i]));
}
}sum
sum adds all elements and returns a primitive result.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int total = IntStream.rangeClosed(1, 100).sum();
System.out.println(total);
}
}average
average returns an OptionalDouble because the stream could be empty.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
double avg = IntStream.of(2, 4, 6, 8).average().getAsDouble();
System.out.println(avg);
}
}min and max
min and max return OptionalInt. Use getAsInt when you know the stream is non-empty.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int hi = IntStream.of(3, 9, 1, 7).max().getAsInt();
int lo = IntStream.of(3, 9, 1, 7).min().getAsInt();
System.out.println(lo + " .. " + hi);
}
}count
count returns the number of elements as a long.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
long n = IntStream.rangeClosed(1, 50).filter(x -> x % 3 == 0).count();
System.out.println(n);
}
}summaryStatistics
summaryStatistics computes count, sum, min, average, and max in a single pass, returning an IntSummaryStatistics.
import java.util.IntSummaryStatistics;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
IntSummaryStatistics stats = IntStream.of(5, 10, 15, 20).summaryStatistics();
System.out.println(stats);
}
}Reading Individual Statistics
The summary object exposes getCount, getSum, getMin, getMax, and getAverage.
import java.util.IntSummaryStatistics;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
IntSummaryStatistics s = IntStream.rangeClosed(1, 10).summaryStatistics();
System.out.println("count=" + s.getCount());
System.out.println("avg=" + s.getAverage());
System.out.println("max=" + s.getMax());
}
}Combining Range and Aggregate
Ranges and aggregates compose naturally: generate, transform, then reduce to a single number.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int sumOfSquares = IntStream.rangeClosed(1, 4)
.map(n -> n * n)
.sum();
System.out.println(sumOfSquares);
}
}LongStream Aggregates
LongStream offers the same range and aggregate methods, returning a LongSummaryStatistics.
import java.util.stream.LongStream;
public class Main {
public static void main(String[] args) {
long sum = LongStream.rangeClosed(1, 1000).sum();
System.out.println(sum);
}
}Quick Check
How do range and rangeClosed differ?
Recap
You generated and summarized primitive ranges:
rangeis exclusive of the end;rangeClosedincludes it.- Aggregates:
sum,average(OptionalDouble),min/max(OptionalInt),count. summaryStatisticscomputes them all in one pass.
Frequently asked questions
Is the “Range and Aggregate Operations” lesson free?
Yes — the full text of “Range and Aggregate Operations” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Range and Aggregate Operations”?
range, sum, average, summaryStatistics. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Range and Aggregate Operations” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- IntStream and LongStream
- Range and Aggregate Operations
- Mapping to and from Object Streams
- Generating Primitive Streams