Print Hollow Diamond in Java
Problem
A hollow diamond is a hollow pyramid stacked directly on top of its own mirror image — because the two diagonal halves already meet at the widest row, no separate solid base is needed to close the shape.
Given a number of rows per half, print a diamond outline of stars with a blank interior.
Java Program
public class HollowDiamond {
public static void main(String[] args) {
int n = 4;
// Top half: hollow pyramid, apex up
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++) {
line.append((j == 1 || j == width) ? "*" : " ");
}
System.out.println(line);
}
// Bottom half: mirror image, apex down — no solid row needed where the halves meet
for (int i = n; i >= 1; 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++) {
line.append((j == 1 || j == width) ? "*" : " ");
}
System.out.println(line);
}
}
}Output
Core Logic
Printing a hollow pyramid for the top half and its exact mirror image for the bottom half joins two triangular outlines into a single diamond silhouette, with nothing extra needed to close the middle.
- 1The top half runs
ifrom 1 ton, printingn - ileading spaces followed by a star at only the first and last position of that row's width. - 2The bottom half runs
ifromndown to 1, using the exact same leading-space and edge-star logic, mirroring the top half row for row. - 3Unlike the standalone hollow triangle or pyramid, neither half gets a solid base — the two halves' widest rows sit directly against each other, so the diamond's left and right points are already closed by the diagonal edges themselves.
- 4Only the very top and very bottom rows end up looking 'solid', and that's just because a width-1 row has nothing but its own single star to print.
n = 4, the shape grows from a single star down to two rows that are eight characters wide, meeting in the middle, then shrinks back to a single star — with the whole interior left visibly blank.Key Point: Adding a solid row where the two halves meet — the way a hollow triangle needs a solid base — would actually break the diamond shape here, since the diagonal edges already close the boundary without one.
Why: Both halves together still visit a number of positions proportional to n², and only the loop counters and per-row StringBuilder are kept.
Key Concepts
Approach 2: Java 8
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class HollowDiamondStream {
public static void main(String[] args) {
int n = 4;
IntStream.concat(
IntStream.rangeClosed(1, n),
IntStream.rangeClosed(1, n).map(k -> n + 1 - k))
.mapToObj(i -> {
int width = 2 * i - 1;
return " ".repeat(n - i) + IntStream.rangeClosed(1, width)
.mapToObj(j -> (j == 1 || j == width) ? "*" : " ")
.collect(Collectors.joining(""));
})
.forEach(System.out::println);
}
}
Output
Core Logic
Both halves build a row from the exact same per-row formula, so concatenating an ascending and a descending row-index stream and mapping each through that one formula reproduces the whole diamond.
- 1
IntStream.rangeClosed(1, n)supplies the top half's row indices,1throughn. - 2
IntStream.rangeClosed(1, n).map(k -> n + 1 - k)supplies the bottom half's row indices,ndown to1, by flipping each index. - 3
IntStream.concat(...)chains the two sequences into one stream of row indices, top half followed by bottom half. - 4Each row index
iis mapped through the identical leading-space-plus-edge-test formula the hollow pyramid's top half already uses, so both halves are built by the same code.
1 (a single star), and the last is also 1 — the diamond's two points — with the widest row (n) appearing twice in a row in the middle, once from each half.Key Point: Concatenating two streams that feed the same per-row formula is the stream equivalent of calling one shared helper method twice with different loop directions — no new logic is needed for the second half.
Why: Both concatenated streams together still evaluate the edge test once per position across all rows, proportional to n², without collecting the full diamond.