Print Hollow Rhombus in Java
Problem
A hollow rhombus keeps the same slanted outline as a solid rhombus, but only the border rows and the first and last column of every row are actually stars — everything inside stays blank.
Given a size n, print an n-row rhombus outline, each row indented one space further than the last.
Java Program
public class HollowRhombus {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
StringBuilder line = new StringBuilder();
for (int j = 1; j < i; j++) line.append(' ');
for (int j = 1; j <= n; j++) {
if (i == 1 || i == n || j == 1 || j == n) { // border row or edge column
line.append('*');
} else {
line.append(' ');
}
}
System.out.println(line);
}
}
}Output
Core Logic
Reusing the solid rhombus's exact indentation, but only placing a star where a row is the first or last one, or a column is the first or last position, leaves the interior blank while keeping the same slanted footprint.
- 1The leading-space calculation —
i - 1spaces for rowi— is identical to the solid rhombus, so the outline slants the same way. - 2Within each row, a star is placed only when
i == 1,i == n,j == 1, orj == n— the top row, bottom row, or either edge column. - 3Every other position inside those boundaries prints a space instead, leaving the middle of the shape empty.
n = 5, row 3 (neither first nor last) prints a star only at j = 1 and j = 5, with three blank positions between them.Key Point: The first and last rows still need every position starred, not just the edges — otherwise the top and bottom of the outline would have gaps instead of a solid closing edge.
Why: The nested loop still visits every one of the n × n positions to decide star or space, even though most of the interior ones resolve to blank.
Key Concepts
Approach 2: Java 8
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class HollowRhombusStream {
public static void main(String[] args) {
int n = 5;
IntStream.rangeClosed(1, n)
.mapToObj(i -> " ".repeat(i - 1) + IntStream.rangeClosed(1, n)
.mapToObj(j -> (i == 1 || i == n || j == 1 || j == n) ? "*" : " ")
.collect(Collectors.joining("")))
.forEach(System.out::println);
}
}
Output
Core Logic
Building each row's leading spaces with String.repeat(), then mapping each position to the same border test used by the solid rhombus's outline, reproduces the shape row by row.
- 1
IntStream.rangeClosed(1, n)produces one stream element per row numberi. - 2
" ".repeat(i - 1)builds the same leading spaces the solid rhombus uses. - 3An inner
IntStream.rangeClosed(1, n)maps every positionjto"*"wheni == 1,i == n,j == 1, orj == n, and to" "otherwise. - 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 border test, so the inner stream produces a star, three blanks, and a closing star.Key Point: The border 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 leading-space indentation and the border formula compose independently.
Why: The nested streams still evaluate the border condition once per grid position, the same total work as the loop version.