Mapping to and from Object Streams
mapToObj and mapToInt.
Mapping to and from Object Streams is a free Java Academy lesson on CoddyKit — lesson 3 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.
Two Stream Worlds
Java has object streams (Stream<T>) and primitive streams (IntStream and friends). Mapping methods let you cross between them.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
System.out.println(IntStream.rangeClosed(1, 3).sum());
}
}mapToObj: Primitive to Object
mapToObj converts each primitive into an object, producing a Stream<T>.
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
List<String> labels = IntStream.rangeClosed(1, 3)
.mapToObj(n -> "item-" + n)
.collect(Collectors.toList());
System.out.println(labels);
}
}mapToInt: Object to Primitive
mapToInt extracts an int from each object, yielding an IntStream ready for numeric aggregation.
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> words = List.of("hi", "hello", "hey");
int totalChars = words.stream()
.mapToInt(String::length)
.sum();
System.out.println(totalChars);
}
}boxed: Primitive to Wrapper Stream
boxed turns an IntStream into a Stream<Integer>, which you need before collecting into a List.
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
List<Integer> list = IntStream.rangeClosed(1, 5)
.boxed()
.collect(Collectors.toList());
System.out.println(list);
}
}mapToLong and mapToDouble
The same pattern applies for other primitives: mapToLong and mapToDouble.
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> prices = List.of("9.99", "19.50", "4.25");
double total = prices.stream()
.mapToDouble(Double::parseDouble)
.sum();
System.out.println(total);
}
}Why mapToInt Beats map
Using map on an object stream keeps it boxed. mapToInt unlocks numeric methods like sum and average while avoiding boxing.
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> nums = List.of(10, 20, 30);
double avg = nums.stream()
.mapToInt(Integer::intValue)
.average()
.getAsDouble();
System.out.println(avg);
}
}Round Trip: Object to Int to Object
You can move both directions in one pipeline: extract numbers, transform, then map back to objects.
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> words = List.of("a", "bb", "ccc");
List<String> report = words.stream()
.mapToInt(String::length)
.mapToObj(len -> "len=" + len)
.collect(Collectors.toList());
System.out.println(report);
}
}asLongStream and asDoubleStream
Within primitive streams you can widen types: asLongStream and asDoubleStream avoid boxing while changing numeric width.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
double avg = IntStream.of(1, 2, 3, 4)
.asDoubleStream()
.average()
.getAsDouble();
System.out.println(avg);
}
}Collecting Primitive Results
To gather primitives into a collection, call boxed first, then a collector.
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
Set<Integer> evens = IntStream.rangeClosed(1, 10)
.filter(n -> n % 2 == 0)
.boxed()
.collect(Collectors.toSet());
System.out.println(evens);
}
}flatMapToInt
flatMapToInt flattens objects into a single IntStream, useful when each object yields multiple numbers.
import java.util.List;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
List<int[]> rows = List.of(new int[]{1, 2}, new int[]{3, 4});
int sum = rows.stream()
.flatMapToInt(Arrays::stream)
.sum();
System.out.println(sum);
}
}Choosing the Right Mapper
Pick by direction:
- Object to primitive:
mapToInt,mapToLong,mapToDouble. - Primitive to object:
mapToObjorboxed.
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> in = List.of("x", "yy");
String out = in.stream()
.mapToInt(String::length)
.mapToObj(Integer::toString)
.collect(Collectors.joining(","));
System.out.println(out);
}
}Quick Check
Which method turns an IntStream into a Stream<Integer>?
Recap
You moved between object and primitive streams:
mapToInt/mapToLong/mapToDoublego object to primitive.mapToObjandboxedgo primitive to object.flatMapToIntflattens many numbers into one IntStream.- Crossing into primitive streams unlocks numeric aggregates without boxing.
Frequently asked questions
Is the “Mapping to and from Object Streams” lesson free?
Yes — the full text of “Mapping to and from Object Streams” 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 “Mapping to and from Object Streams”?
mapToObj and mapToInt. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Mapping to and from Object Streams” 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