How do I list System properties alphabetically?
Author: Deron Eriksson
Description: This Java tutorial shows how to list System properties alphabetically.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1

When enumerating over system properties, by default the results are not returned in alphabetical order since the underlying data structure is a Hashtable. If we'd like the results to be returned alphabetically, one easy way of accomplishing this is via a TreeMap object, which orders its results.

The ListSystemPropertiesAlphabetically class shows one way to output the system properties alphabetically. A TreeMap object is created with its constructor taking the system Properties object as its parameter. The SortedMap interface gives us access to the TreeMap object. We obtain the set of keys from the SortedMap, and then iterate over these keys. For each key, we obtain its value from the system Properties object, and we output this key/value pair to standard output.

package test;

import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

public class ListSystemPropertiesAlphabetically {
	public static void main(String[] args) {
		Properties systemProperties = System.getProperties();
		SortedMap sortedSystemProperties = new TreeMap(systemProperties);
		Set keySet = sortedSystemProperties.keySet();
		Iterator iterator = keySet.iterator();
		while (iterator.hasNext()) {
			String propertyName = (String);
			String propertyValue = systemProperties.getProperty(propertyName);
			System.out.println(propertyName + ": " + propertyValue);

The results of the execution of ListSystemPropertiesAlphabetically on my system are displayed below.


file.encoding: Cp1252
file.separator: \
java.awt.graphicsenv: sun.awt.Win32GraphicsEnvironment
java.class.path: C:\projects\workspace\testing\bin
java.class.version: 49.0
java.endorsed.dirs: C:\jdk1.5.0_09\jre\lib\endorsed
java.ext.dirs: C:\jdk1.5.0_09\jre\lib\ext
java.home: C:\jdk1.5.0_09\jre C:\DOCUME~1\Deron\LOCALS~1\Temp\
java.library.path: C:\jdk1.5.0_09\jre\bin;.;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\QuickTime\QTSystem\;C:\jdk1.5.0_09\bin;C:\maven-2.0.4\bin;C:\jadnt158; Java(TM) 2 Runtime Environment, Standard Edition
java.runtime.version: 1.5.0_09-b03 Java Platform API Specification
java.specification.vendor: Sun Microsystems Inc.
java.specification.version: 1.5
java.vendor: Sun Microsystems Inc.
java.version: 1.5.0_09 mixed mode Java HotSpot(TM) Client VM Java Virtual Machine Specification
java.vm.specification.vendor: Sun Microsystems Inc.
java.vm.specification.version: 1.0
java.vm.vendor: Sun Microsystems Inc.
java.vm.version: 1.5.0_09-b03

os.arch: x86 Windows XP
os.version: 5.1
path.separator: ; 32
sun.boot.class.path: C:\jdk1.5.0_09\jre\lib\rt.jar;C:\jdk1.5.0_09\jre\lib\i18n.jar;C:\jdk1.5.0_09\jre\lib\sunrsasign.jar;C:\jdk1.5.0_09\jre\lib\jsse.jar;C:\jdk1.5.0_09\jre\lib\jce.jar;C:\jdk1.5.0_09\jre\lib\charsets.jar;C:\jdk1.5.0_09\jre\classes
sun.boot.library.path: C:\jdk1.5.0_09\jre\bin
sun.cpu.endian: little
sun.cpu.isalist: pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86
sun.desktop: windows UnicodeLittle
sun.jnu.encoding: Cp1252 HotSpot Client Compiler
sun.os.patch.level: Service Pack 2 US
user.dir: C:\projects\workspace\testing
user.home: C:\Documents and Settings\Deron
user.language: en Deron

Note that Hashtable is synchronized and TreeMap is not, since this can have various consequences in your application if you desire to use the TreeMap for anything besides property display. Also, the example's instantiation of TreeMap doesn't use generics, so EclipseSW will throw a warning about this line, although it works fine.