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.

ListSystemPropertiesAlphabetically.java

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) iterator.next();
			String propertyValue = systemProperties.getProperty(propertyName);
			System.out.println(propertyName + ": " + propertyValue);
		}
	}
}

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

results.txt

awt.toolkit: sun.awt.windows.WToolkit
file.encoding: Cp1252
file.encoding.pkg: sun.io
file.separator: \
java.awt.graphicsenv: sun.awt.Win32GraphicsEnvironment
java.awt.printerjob: sun.awt.windows.WPrinterJob
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
java.io.tmpdir: 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.runtime.name: Java(TM) 2 Runtime Environment, Standard Edition
java.runtime.version: 1.5.0_09-b03
java.specification.name: Java Platform API Specification
java.specification.vendor: Sun Microsystems Inc.
java.specification.version: 1.5
java.vendor: Sun Microsystems Inc.
java.vendor.url: http://java.sun.com/
java.vendor.url.bug: http://java.sun.com/cgi-bin/bugreport.cgi
java.version: 1.5.0_09
java.vm.info: mixed mode
java.vm.name: Java HotSpot(TM) Client VM
java.vm.specification.name: 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
line.separator: 

os.arch: x86
os.name: Windows XP
os.version: 5.1
path.separator: ;
sun.arch.data.model: 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
sun.io.unicode.encoding: UnicodeLittle
sun.jnu.encoding: Cp1252
sun.management.compiler: HotSpot Client Compiler
sun.os.patch.level: Service Pack 2
user.country: US
user.dir: C:\projects\workspace\testing
user.home: C:\Documents and Settings\Deron
user.language: en
user.name: Deron
user.timezone: 
user.variant: 

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.