Java ListFormat
Last modified: June 16, 2025
In this article, we demonstrate how to effectively utilize the Java
ListFormat
class. We explore its application with various
formatting styles and provide practical examples of using
ListFormat
to format lists, collections, and arrays in a
locale-sensitive manner.
The ListFormat
class formats a list of objects according to
locale-specific rules. Introduced in Java 22, this class offers developers a
modern, internationalized approach to list formatting. It handles conjunctions
and separators automatically based on the specified locale and style.
public final class ListFormat extends Format
The snippet above shows the class declaration of ListFormat
, which
extends the Format
class. It provides methods to format lists of
objects into human-readable strings with proper separators and conjunctions.
ListFormat Types
The ListFormat
class defines various formatting styles through
the ListFormat.Type
enum. These styles determine how list elements
are structured and connected in output. The available types are
STANDARD
, OR
, and UNIT
.
public enum ListFormat.Type { STANDARD, OR, UNIT }
The ListFormat.Type
enum specifies different ways to format lists.
The STANDARD
type uses "and" as a conjunction between items,
OR
replaces "and" with "or" for alternate choices, and
UNIT
formats items without any conjunctions, suitable for
unit-based lists. Each type dynamically adjusts to locale-specific linguistic
rules.
ListFormat Styles
In addition to formatting types, the ListFormat
class offers
multiple styles through the ListFormat.Style
enum.
These are FULL
, SHORT
, and NARROW
.
public enum ListFormat.Style { FULL, SHORT, NARROW }The styles control the level of detail in the formatted output, such as whether to include full words for conjunctions or use abbreviations. The
FULL
style provides complete words for conjunctions, while
SHORT
and NARROW
use abbreviated forms.
Basic ListFormat Usage
This example demonstrates the basic usage of ListFormat
with a
simple list of strings. We use the default locale and standard formatting
style to create a properly formatted list output.
void main() { List<String> fruits = List.of("apple", "banana", "cherry", "date"); ListFormat listFormat = ListFormat.getInstance(Locale.US, ListFormat.Type.STANDARD, ListFormat.Style.FULL); String formatted = listFormat.format(fruits); System.out.println(formatted); }
In this example, we create a list of fruit names and format them using the
standard style. The ListFormat.getInstance
method creates a
formatter for the US locale with standard type and full style. The output will
be "apple, banana, cherry, and date" with proper English conjunctions.
Different Formatting Types
This example showcases all three available formatting types of
ListFormat
. We format the same list using different types to
demonstrate how each affects the output format and conjunction usage.
void main() { List<String> colors = List.of("red", "blue", "green"); ListFormat standardFormat = ListFormat.getInstance(Locale.US, ListFormat.Type.STANDARD, ListFormat.Style.FULL); ListFormat orFormat = ListFormat.getInstance(Locale.US, ListFormat.Type.OR, ListFormat.Style.FULL); ListFormat unitFormat = ListFormat.getInstance(Locale.US, ListFormat.Type.UNIT, ListFormat.Style.FULL); System.out.println("Standard: " + standardFormat.format(colors)); System.out.println("Or: " + orFormat.format(colors)); System.out.println("Unit: " + unitFormat.format(colors)); }
Here, we demonstrate all three formatting types with the same data. The standard type produces "red, blue, and green", the OR type produces "red, blue, or green", and the unit type produces "red, blue, green" without conjunctions. Each type serves different purposes in text formatting.
Comparing ListFormat Styles
This example demonstrates the differences between all three
ListFormat.Style
options: FULL
, SHORT
,
and NARROW
. We format the same list using different styles to
show how the level of detail varies in the output.
void main() { List<String> programming = List.of("Java", "Python", "JavaScript", "C++"); ListFormat fullStyle = ListFormat.getInstance(Locale.US, ListFormat.Type.STANDARD, ListFormat.Style.FULL); ListFormat shortStyle = ListFormat.getInstance(Locale.US, ListFormat.Type.STANDARD, ListFormat.Style.SHORT); ListFormat narrowStyle = ListFormat.getInstance(Locale.US, ListFormat.Type.STANDARD, ListFormat.Style.NARROW); System.out.println("Full style: " + fullStyle.format(programming)); System.out.println("Short style: " + shortStyle.format(programming)); System.out.println("Narrow style: " + narrowStyle.format(programming)); }
This comparison shows how the same list can be formatted with varying levels of
detail. The FULL
style provides complete words for conjunctions
and separators, SHORT
uses abbreviated forms where applicable, and
NARROW
provides the most compact formatting. The differences
become more apparent in locales that have distinct abbreviated forms for
conjunctions and separators.
Locale-Specific Formatting
The ListFormat
class adapts its output to different locales,
changing separators and conjunctions according to language rules. This example
demonstrates how the same list appears differently across various locales.
void main() { List<String> countries = List.of("France", "Germany", "Italy"); ListFormat englishFormat = ListFormat.getInstance(Locale.ENGLISH, ListFormat.Type.STANDARD, ListFormat.Style.FULL); ListFormat frenchFormat = ListFormat.getInstance(Locale.FRENCH, ListFormat.Type.STANDARD, ListFormat.Style.FULL); ListFormat germanFormat = ListFormat.getInstance(Locale.GERMAN, ListFormat.Type.STANDARD, ListFormat.Style.FULL); System.out.println("English: " + englishFormat.format(countries)); System.out.println("French: " + frenchFormat.format(countries)); System.out.println("German: " + germanFormat.format(countries)); }
In this example, we format the same list of countries using different locales.
Each locale applies its own linguistic rules for list formatting. The English
version uses "and", while French and German use their respective conjunctions
("et" and "und"). This demonstrates the internationalization capabilities of
ListFormat
.
Formatting List and Arrays
The ListFormat
class can format lists and arrays.
void main() { String[] arrayData = { "Monday", "Tuesday", "Wednesday" }; List<String> listData = List.of("January", "February", "March"); ListFormat formatter = ListFormat.getInstance(Locale.US, ListFormat.Type.STANDARD, ListFormat.Style.FULL); System.out.println("Array: " + formatter.format(arrayData)); System.out.println("List: " + formatter.format(listData)); }
In this example we format an array and a list using the same formatter instance. T
Custom Object Formatting
The ListFormat
class works with custom objects by calling their
toString
method. This example shows how to format lists containing
custom objects and how to control their string representation for better
formatting results.
class Product { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } @Override public String toString() { return name + " ($" + String.format("%.2f", price) + ")"; } } void main() { List<Product> products = List.of( new Product("Laptop", 999.99), new Product("Mouse", 25.50), new Product("Keyboard", 75.00)); ListFormat formatter = ListFormat.getInstance(Locale.US, ListFormat.Type.STANDARD, ListFormat.Style.FULL); String formatted = formatter.format(products); System.out.println("Products: " + formatted); }
In this scenario, we define a Product
class with custom
toString
implementation. The ListFormat
automatically
uses this method when formatting the list of products. We create a list of
Product
objects and format them using the standard style. The
output shows each product with its price, properly formatted with commas and
conjunctions.
Source
Java Language Basics - Tutorial
In this article, we have thoroughly explored the Java ListFormat
class. We introduced the concept of locale-specific list formatting and
demonstrated its use across different styles, locales, and data structures,
providing a comprehensive understanding of this powerful formatting tool.
Author
List all Java tutorials.