You can obtain a Properties instance of the JVM properties using System.getProperties(); how would you go about using Java 8 code to print all properties to the console?
- 119,121
- 33
- 254
- 329
-
Nobody should be running JDK 8 anymore. You need to upgrade to LTS version JDK 17 immediately. – duffymo Aug 09 '23 at 12:32
6 Answers
One solution:
public final class Foo
{
private static void printProperty(final Object key, final Object value)
{
System.out.println(key + ": " + value);
}
public static void main(final String... args)
{
System.getProperties().forEach(Foo::printProperty);
}
}
Rundown:
PropertiesextendsHashtable<Object, Object>which itself implementsMap<Object, Object>;Maphas a.forEach()method whose argument is aBiConsumer;BiConsumeris a functional interface;- static method
printProperty()of classFoohappens to have the same signature as aBiConsumer<Object, Object>: its "return value" isvoid, its first argument isObject, its second argument isObject; - we can therefore use
Foo::printPropertyas a method reference.
A shorter version would be:
public final class ShorterFoo
{
public static void main(final String... args)
{
System.getProperties()
.forEach((key, value) -> System.out.println(key + ": " + value));
}
}
At runtime, this would not make a difference. Note the type inference in the second example: the compiler can infer that key and value are of type Object. Another way to write this "anonymous lambda" would have been:
(Object key, Object value) -> System.out.println(key + ": " + value)
(not so) Side note: even though it is a little outdated, you really want to watch this video (yes, it's one hour long; yes, it is worth watching it all).
(not so) Side note 2: you may have noticed that Map's .forEach() mentions a default implementation; this means that your custom Map implementations, or other implementations from external libraries, will be able to use .forEach() (for instance, Guava's ImmutableMaps). Many such methods on Java collections exist; do not hesitate to use these "new methods" on "old dogs".
- 119,121
- 33
- 254
- 329
-
-
1@KirkWoll sort of a pun; that is, I deem these side notes as important as the answer itself – fge Nov 05 '14 at 01:40
@fge has missed one very short version that admittedly depends on the toString implementation of Map.Entry.
public class VeryShortFoo {
public static void main(String... args) {
System.getProperties().entrySet().forEach(System.out::println);
}
}
- Here, the
entrySetis streamed and eachMap.Entryis printed with a reference toout.println. Map.Entryimplementations oftoStringgenerally returngetKey() + "=" + getValue().
Here's another one I quite like.
public class ElegantFoo {
public static void main(String... args) {
System.getProperties().entrySet().stream()
.map(e -> e.getKey() + ": " + e.getValue())
.forEach(System.out::println);
}
}
- The
entrySetis streamed again (this time explicitly with a call tostream). Stream#mapperforms a 1:1 conversion from elements of one type to elements of another. Here, it turns aStream<Map.Entry>in to aStream<String>.- The
Stream<String>is printed.
- 37,180
- 14
- 90
- 125
-
Nice one... In fact I haven't even tried that. Of note here is that it really "calls" the `.forEach()` method on a `Set`. (also, pure English question: wouldn't you write "admittedly" instead of "admittingly"?) – fge Nov 05 '14 at 01:36
-
Might I suggest that you highlight the "rundown" of your code snippets like I did? – fge Nov 05 '14 at 03:47
In Java 8, the Properties class inherits a new method from HashTable called forEach. This new method accepts functions (functional interfaces) to be passed to it as arguments. To be more specific, it accepts the functional interface BiConsumer<T,U>. This functional interface's functional method is accept(T t, U u). In Java 8, all functional interfaces can be written as Lambda expressions. Therefore, here is how we would display all properties in a Property instance:
Properties vmProps = System.getProperties();
vmProps.forEach((t,u) -> System.out.println("Property: " + t + "\nValue: " + u + "\n"));
- 670
- 2
- 8
- 18
Sorted by key using a TreeSet. This makes the output easier to read.
printMap(System.getProperties());
printMap(System.getenv());
public static void printMap(Map properties) {
new TreeSet<>(properties.keySet()).forEach((k) -> {
System.out.println(k + " : " + properties.get(k));
});
}
- 8,526
- 6
- 32
- 72
public static void main(String[] args) {
System.getProperties().keySet().forEach((k)-> System.out.println(k + " :::: "+ System.getProperty(k.toString())) );
}
Shortest Version for me :D The following for System Variables
System.getenv().forEach((String env, String val) -> System.out.println(new StringBuilder(env).append(" =>> ").append(val).toString()));
- 1
- 2