IntStream and LongStream
Avoid boxing in streams.
IntStream and LongStream is a free Java Academy lesson on CoddyKit — lesson 1 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.
Why Primitive Streams Exist
Stream<Integer> boxes every element into an Integer object. IntStream, LongStream, and DoubleStream work on raw primitives to avoid boxing overhead.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int sum = IntStream.of(1, 2, 3, 4).sum();
System.out.println(sum);
}
}Creating an IntStream
Use IntStream.of for explicit values, or Arrays.stream over an int[].
import java.util.Arrays;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int[] data = {10, 20, 30};
IntStream s = Arrays.stream(data);
System.out.println(s.max().getAsInt());
}
}LongStream for Large Numbers
LongStream mirrors IntStream but works with long values, avoiding overflow on big sums.
import java.util.stream.LongStream;
public class Main {
public static void main(String[] args) {
long total = LongStream.of(1_000_000_000L, 2_000_000_000L).sum();
System.out.println(total);
}
}Specialized Terminal Operations
Primitive streams add numeric methods that object streams lack: sum, min, max, average.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
IntStream nums = IntStream.of(4, 8, 15, 16, 23, 42);
System.out.println(nums.average().getAsDouble());
}
}OptionalInt Results
min and max return OptionalInt because the stream might be empty. Use getAsInt or orElse.
import java.util.OptionalInt;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
OptionalInt result = IntStream.empty().max();
System.out.println(result.orElse(-1));
}
}map Stays Primitive
map on an IntStream takes an IntUnaryOperator and returns another IntStream, so no boxing happens.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int sumOfSquares = IntStream.of(1, 2, 3)
.map(n -> n * n)
.sum();
System.out.println(sumOfSquares);
}
}filter on Primitive Streams
filter takes an IntPredicate and keeps matching primitives.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
long evens = IntStream.of(1, 2, 3, 4, 5, 6)
.filter(n -> n % 2 == 0)
.count();
System.out.println(evens);
}
}reduce on IntStream
reduce folds the stream using an IntBinaryOperator. With an identity value it returns an int directly.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int product = IntStream.rangeClosed(1, 5)
.reduce(1, (a, b) -> a * b);
System.out.println(product);
}
}forEach Over Primitives
forEach takes an IntConsumer, again avoiding boxing.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
IntStream.of(7, 8, 9).forEach(n -> System.out.println(n));
}
}Collecting to an Array
toArray returns a primitive int[], the natural counterpart of an IntStream.
import java.util.Arrays;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int[] doubled = IntStream.of(1, 2, 3).map(n -> n * 2).toArray();
System.out.println(Arrays.toString(doubled));
}
}Converting Between Int and Long
IntStream.asLongStream widens primitives to long without boxing.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
long sum = IntStream.of(1, 2, 3).asLongStream().sum();
System.out.println(sum);
}
}Quick Check
What is the main reason to use IntStream instead of Stream<Integer>?
Recap
You used primitive streams to skip boxing:
IntStreamandLongStreamhold raw primitives.- They add numeric terminals:
sum,min,max,average. minandmaxreturnOptionalInt.map,filter,reduce, andtoArrayall stay primitive.
Frequently asked questions
Is the “IntStream and LongStream” lesson free?
Yes — the full text of “IntStream and LongStream” 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 “IntStream and LongStream”?
Avoid boxing in streams. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “IntStream and LongStream” 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