The static method System.getProperties() returns a Properties object containing the System Properties as key-value pairs.
In the SO post How would I print the JVM's system properties using Java 8 and lambdas? Radiodef provided a solution which works fine, and it can be enhanced to also sort the output:
System.getProperties()
.entrySet()
.stream()
.map(e -> e.getKey() + ": " + e.getValue())
.sorted()
.forEach(System.out::println);
Here is a small portion of the sorted output produced by that statement:
I tried unsucessfully to amend the statement above so that the property values in the output are left-aligned. Since java.vm.specification.version happens to be the longest System Properties key, the formatted presentation of the output shown above should look like this:
Obviously the tricky part is to somehow know the length of the longest key prior to formatting the output. Any suggestions on how to achieve this in a single statement using a Stream, or is it impossible?
Update:
I forgot to state in the original post that an additional constraint is that the solution should also work using a parallel stream. My apologies for the oversight.