Java ProgramsPatternsPrint Hollow Diamond

Print Hollow Diamond in Java

intermediate·  Patterns  ·  Star Patterns

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.

Input
n = 4
Output
* * * * * * * * * * * * * *

Java Program

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

How It Works
  1. 1The top half runs i from 1 to n, printing n - i leading spaces followed by a star at only the first and last position of that row's width.
  2. 2The bottom half runs i from n down to 1, using the exact same leading-space and edge-star logic, mirroring the top half row for row.
  3. 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.
  4. 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.
For 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.

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

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

nested for loopmirrored halvesleading spaces

Approach 2: Java 8

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

How It Works
  1. 1IntStream.rangeClosed(1, n) supplies the top half's row indices, 1 through n.
  2. 2IntStream.rangeClosed(1, n).map(k -> n + 1 - k) supplies the bottom half's row indices, n down to 1, by flipping each index.
  3. 3IntStream.concat(...) chains the two sequences into one stream of row indices, top half followed by bottom half.
  4. 4Each row index i is 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.
The first row index in the stream is 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.

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

Why: Both concatenated streams together still evaluate the edge test once per position across all rows, proportional to n², without collecting the full diamond.

Key Concepts

StreamIntStream.concat()IntStream.range()Collectors.joining()

Related Programs