Definition
- Integer::parseInt references the static method
parseInt(String)as a functional value. - Matches
Function(for map) orToIntFunction(for mapToInt). - mapToInt produces a primitive IntStream for efficient numeric aggregation.
- NumberFormatException on malformed input aborts the stream.
- No receiver object is involved: the argument goes directly to the static method.
Priya at the National Bank of India receives transaction amounts as text and needs them as numbers to add up. Instead of writing 'take the text and parse it', she points at the built-in parse tool and runs each text through it.
Understanding the Concepts
How do you convert string numbers to integers?
Use Integer::parseInt as a static method reference in map or mapToInt.
package com.bank.ops;
import java.util.*;
import java.util.stream.Collectors;
public class ParseStringsToInts {
public static void main(String[] args) {
List<String> amounts = List.of("100", "250", "75", "300");
// Stream<Integer> via map
List<Integer> boxed = amounts.stream()
.map(Integer::parseInt)
.collect(Collectors.toList());
System.out.println("Boxed: " + boxed);
// Primitive IntStream via mapToInt
int total = amounts.stream()
.mapToInt(Integer::parseInt)
.sum();
System.out.println("Total: " + total);
}
}
Boxed: [100, 250, 75, 300]
Total: 725
What is the difference between map and mapToInt?
| Method | Result type | Boxing | Numeric operations |
|---|---|---|---|
| map(Integer::parseInt) | Stream
| mapToInt(Integer::parseInt) | IntStream (primitive) | No | sum(), average(), max() |
Use mapToInt when you need numeric aggregations to avoid boxing overhead.
How do you handle malformed input?
parseInt throws NumberFormatException on non-numeric strings, aborting the entire stream.
// WRONG: one bad value crashes the whole stream
List<String> dirty = List.of("100", "abc", "300");
int total = dirty.stream()
.mapToInt(Integer::parseInt) // throws on "abc"!
.sum();
// CORRECT: wrap in a helper that returns Optional
import java.util.Optional;
import java.util.OptionalInt;
int safeTotal = dirty.stream()
.map(s -> {
try {
return OptionalInt.of(Integer.parseInt(s));
} catch (NumberFormatException e) {
return OptionalInt.empty();
}
})
.filter(OptionalInt::isPresent)
.mapToInt(OptionalInt::getAsInt)
.sum();
System.out.println("Safe total: " + safeTotal); // 400
What are common mistakes?
Mistake: using Integer::parseInt when Integer::valueOf is more appropriate.
// parseInt returns primitive int
int primitive = Integer.parseInt("42");
// valueOf returns Integer object (cached for -128 to 127)
Integer boxed = Integer.valueOf("42");
// In a stream with map, both work; mapToInt requires ToIntFunction.
What should a senior engineer know?
- A static method reference has no receiver; the argument flows directly to the static method.
- parseInt is the right choice for mapToInt (returns primitive int). valueOf is better when you want the cached Integer object.
- For production data pipelines, never use a bare parseInt in a stream without error handling. One bad record should not abort the entire batch.
- Choosing a static method reference assumes the inputs are clean or that failing fast is acceptable.
What are common interview follow-up questions?
- "How do you convert to Double instead?" Use Double::parseDouble or mapToDouble.
- "How do you handle leading/trailing whitespace?" Trim first: .map(String::trim).mapToInt(Integer::parseInt).
- "What about very large numbers?" parseInt overflows for values outside int range; use Long::parseLong or BigInteger.
Visual Overview
Stream<String> ["100", "250", "75", "300"]
│
│ .mapToInt(Integer::parseInt)
│
│ Static method: no receiver
│ Each string → Integer.parseInt(string) → int
▼
IntStream [100, 250, 75, 300] (primitive, no boxing)
│
│ .sum()
▼
int: 725
Key Takeaway
A static method reference passes the argument directly to the static method with no receiver. Integer::parseInt matches a function from String to int, so mapToInt(Integer::parseInt) parses each numeric string into a primitive int and produces an IntStream ready for sum(), average(), and other numeric operations.
The critical production concern is error handling. parseInt throws NumberFormatException on malformed input, and a method reference has no room for a try-catch, so one bad record aborts the entire stream. At the National Bank of India, the transaction import wrapped parsing in a helper returning OptionalInt and filtered out failures.
Use mapToInt for primitive efficiency and numeric aggregation; use map for Stream
Remember one sentence: Integer::parseInt is a static method reference for parsing strings to ints, and in production always guard against NumberFormatException.
---