Java ProgramsPatternsPrint Hollow Pyramid

Print Hollow Pyramid in Java

intermediate·  Patterns  ·  Star Patterns

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.

Input
n = 5
Output
* * * * * * * *********

Java Program

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

How It Works
  1. 1Each row i gets n - i leading spaces, the same centering technique used to build a solid pyramid.
  2. 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 when i == n, the base row.
  3. 3Every other position inside the width — on a non-base row — prints a blank space.
  4. 4The leading spaces and the row's own left edge together keep every row's stars aligned into a triangular silhouette.
For 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.

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

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

nested for loopleading spacesborder condition

Approach 2: Java 8

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

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

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

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.

Key Concepts

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

Related Programs