Print Hollow Pyramid in Java
Problem
A hollow pyramid combines a centered pyramid's leading spaces with a hollow triangle's edge-only stars, closing the shape with a solid base row.
Given a number of rows, print a centered pyramid outline of stars with a blank interior and a solid base.
Java Program
public class HollowPyramid {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
StringBuilder line = new StringBuilder();
for (int s = 1; s <= n - i; s++) {
line.append(" ");
}
int width = 2 * i - 1;
for (int j = 1; j <= width; j++) {
if (j == 1 || j == width || i == n) { // edge position, or the solid base row
line.append("*");
} else {
line.append(" ");
}
}
System.out.println(line);
}
}
}Output
Core Logic
Combining a pyramid's centering spaces with a hollow triangle's edge-only rule prints a triangle shape whose interior is blank but whose silhouette still reads as a pyramid.
- 1Each row
igetsn - ileading spaces, the same centering technique used to build a solid pyramid. - 2The row's star width is
2i - 1, and within that width a star is printed only at the first position, the last position, or wheni == n, the base row. - 3Every other position inside the width — on a non-base row — prints a blank space.
- 4The leading spaces and the row's own left edge together keep every row's stars aligned into a triangular silhouette.
n = 5, the first four rows show two slanted edge stars each, framed by shrinking leading spaces, while the fifth row is a solid run of nine stars.Key Point: This is the same base-row exception used by the plain hollow triangle — a pyramid without a closed base would look like two floating diagonal lines instead of an enclosed shape.
Why: The nested loop still visits every position across the pyramid's rows, a total proportional to n², with no growing storage beyond the loop counters.
Key Concepts
Approach 2: Java 8
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class HollowPyramidStream {
public static void main(String[] args) {
int n = 5;
IntStream.rangeClosed(1, n)
.mapToObj(i -> {
int width = 2 * i - 1;
return " ".repeat(n - i) + IntStream.rangeClosed(1, width)
.mapToObj(j -> (j == 1 || j == width || i == n) ? "*" : " ")
.collect(Collectors.joining(""));
})
.forEach(System.out::println);
}
}
Output
Core Logic
Building each row's leading spaces with String.repeat(), then mapping each star position to the same edge-or-base test, reproduces the outline row by row.
- 1
IntStream.rangeClosed(1, n)produces one stream element per row numberi. - 2
" ".repeat(n - i)builds that row's leading spaces, the same centering technique used for a solid pyramid. - 3An inner
IntStream.rangeClosed(1, width)maps every positionjto"*"whenj == 1,j == width, ori == n, and to" "otherwise — the same condition the loop version tests. - 4
Collectors.joining("")concatenates that row's characters with no separator, matching the loop version's plain character append.
j = 1 and j = 5 satisfy the edge test, so the inner stream produces a star, three blanks, and a closing star.Key Point: The edge-or-base test reads only its own i and j, so it maps onto a stream exactly as cleanly as it fits inside a nested loop — the base-row special case is still just one more condition in the same formula.
Why: The nested streams still evaluate the edge-or-base condition once per position across the pyramid's rows, the same total work as the loop version.