How can I print JVM system properties using Java 8 and lambdas?

You can get an instance of the Properties of JVM properties with System.getProperties() ; how would you use Java 8 code to print all the console properties?

+8
java lambda java-8
source share
5 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); } } 

Run over:

  • Properties extends Hashtable<Object, Object> which itself implements Map<Object, Object> ;
  • Map is the .forEach() method, whose argument is BiConsumer ;
  • BiConsumer is a functional interface ;
  • The static printProperty() method of the Foo class has the same signature as BiConsumer<Object, Object> : its "return value" is void , its first argument is Object , and the second is Object ;
  • therefore we can use Foo::printProperty as a reference to a method.

The shortened version will 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 will not matter. Notice the type inference in the second example: the compiler can determine that key and value are of type Object . Another way to write this โ€œanonymous lambdaโ€ would be:

 (Object key, Object value) -> System.out.println(key + ": " + value) 

(not so) Note: even if it's a little outdated, you really want to watch this video (yes, it lasts one hour; yes, it's worth it).


(not this way). Note 2: you may have noticed that Map .forEach() mentions the default implementation ; this means that your custom Map implementations or other implementations from external libraries will be able to use .forEach() (e.g. Guava ImmutableMap s). There are many such methods in Java collections; Feel free to use these "new methods" on "old dogs".

+8
source share

@fge missed one very short version, which admittedly depends on the implementation of toString Map.Entry .

 public class VeryShortFoo { public static void main(String... args) { System.getProperties().entrySet().forEach(System.out::println); } } 
  • Here, the entrySet is entrySet , and each Map.Entry is printed with reference to out.println .
  • Map.Entry implementation of toString usually returns getKey() + "=" + getValue() .

Here is another one that I really 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 entrySet stream is entrySet (this time explicitly with a call to stream ).
  • Stream#map converts 1: 1 from elements of one type to elements of another. Here it turns a Stream<Map.Entry> in into a Stream<String> .
  • Printed Stream<String> .
+5
source share

In Java 8, the Properties class inherits a new method from HashTable called forEach . This new method accepts functions (functional interfaces) that should be passed to it as arguments. To be more specific, it adopts the BiConsumer<T,U> functional interface. This functional functional interface accept(T t, U u) . In Java 8, all functional interfaces can be written as lambda expressions. Thus, this is how we will display all the properties in the Property instance:

 Properties vmProps = System.getProperties(); vmProps.forEach((t,u) -> System.out.println("Property: " + t + "\nValue: " + u + "\n")); 
0
source share

Key sorting (makes it useful)

 printProperties(System.getProperties()); public static void printProperties(Properties properties) { new TreeSet<>(properties.keySet()).forEach((k) -> { System.out.println(k + " : " + properties.get(k)); }); } 
0
source share
 System.getProperties().list(System.out); 
0
source share

All Articles