Java ProgramsPatternsPrint Hollow Rhombus

Print Hollow Rhombus in Java

intermediate·  Patterns  ·  Star Patterns

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.

Input
5
Output
***** * * * * * * *****

Java Program

Java
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.

How It Works
  1. 1The leading-space calculation — i - 1 spaces for row i — is identical to the solid rhombus, so the outline slants the same way.
  2. 2Within each row, a star is placed only when i == 1, i == n, j == 1, or j == n — the top row, bottom row, or either edge column.
  3. 3Every other position inside those boundaries prints a space instead, leaving the middle of the shape empty.
For 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.

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

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

nested for loopborder checkleading spaces

Approach 2: Java 8

Java
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.

How It Works
  1. 1IntStream.rangeClosed(1, n) produces one stream element per row number i.
  2. 2" ".repeat(i - 1) builds the same leading spaces the solid rhombus uses.
  3. 3An inner IntStream.rangeClosed(1, n) maps every position j to "*" when i == 1, i == n, j == 1, or j == n, and to " " otherwise.
  4. 4Collectors.joining("") concatenates that row's characters with no separator, matching the loop version's plain character append.
For row 3 (neither first nor last), only 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.

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

Why: The nested streams still evaluate the border condition once per grid position, the same total work as the loop version.

Key Concepts

StreamString.repeat()IntStream.rangeClosed()Collectors.joining()

Related Programs