Home » MBA Questions

Category Archives: MBA Questions

How to list System properties in Sorted Way

We can use TreeMap properties to list System properties in Sorted Way. System.getProperties() returns a Properties object which contains key value pairs only of String type. We can pass this Properties object in TreeMap constructor. TreeMap stores keys in the sorted manner. Since, It is of type String hence we don’t need to do manual sorting. TreeMap , by default will sort it.

Below is the demo program to list System properties in Sorted Way:

package com.mathapachi;

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

public class ListSystemPropertiesSorted {
    //Properties object contains key-value pairs.Key value both will be string.
	public static void main(String[] args) {
		 Properties properties = System.getProperties(); 
		 TreeMap map = new TreeMap(properties);
		 Set entrySet = map.entrySet(); 
		 Iterator iterator = entrySet.iterator(); 
		 while(iterator.hasNext()){
			 System.out.println(iterator.next()); 
		 }
	}

}

This will print the properties in a sorted manner based on the keys. Below is the output of the above program.

Output:

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:\Users\deepak\workspace\HelloWorld\bin
java.class.version=49.0
java.endorsed.dirs=C:\Users\deepak\AppData\Roaming\MyEclipse 6.5\jre\lib\endorsed
java.ext.dirs=C:\Users\deepak\AppData\Roaming\MyEclipse 6.5\jre\lib\ext
java.home=C:\Users\deepak\AppData\Roaming\MyEclipse 6.5\jre
java.io.tmpdir=C:\Users\deepak\AppData\Local\Temp\
java.library.path=C:\Users\deepak\AppData\Roaming\MyEclipse 6.5\jre\bin;.;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Hewlett-Packard\SimplePass\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\Skype\Phone\
java.runtime.name=Java(TM) 2 Runtime Environment, Standard Edition
java.runtime.version=1.5.0_11-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_11
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_11-b03
line.separator=

os.arch=x86
os.name=Windows Vista
os.version=6.2
path.separator=;
sun.arch.data.model=32
sun.boot.class.path=C:\Users\deepak\AppData\Roaming\MyEclipse 6.5\jre\lib\rt.jar;C:\Users\deepak\AppData\Roaming\MyEclipse 6.5\jre\lib\i18n.jar;C:\Users\deepak\AppData\Roaming\MyEclipse 6.5\jre\lib\sunrsasign.jar;C:\Users\deepak\AppData\Roaming\MyEclipse 6.5\jre\lib\jsse.jar;C:\Users\deepak\AppData\Roaming\MyEclipse 6.5\jre\lib\jce.jar;C:\Users\deepak\AppData\Roaming\MyEclipse 6.5\jre\lib\charsets.jar;C:\Users\deepak\AppData\Roaming\MyEclipse 6.5\jre\classes
sun.boot.library.path=C:\Users\deepak\AppData\Roaming\MyEclipse 6.5\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.java.launcher=SUN_STANDARD
sun.jnu.encoding=Cp1252
sun.management.compiler=HotSpot Client Compiler
sun.os.patch.level=
user.country=
user.dir=C:\Users\deepak\workspace\HelloWorld
user.home=C:\Users\deepak
user.language=en
user.name=deepak
user.timezone=
user.variant=