I'm trying to read Java system properties at runtime, filter based on an includes list (hard coded here but normally injected in via properties file) and finally sorted by key and converted to a Map<String, String>. This is what I've come up with but not sure if it's the most elegant solution.
final List<String> includes = Arrays
.asList("java.version", "PID", "os.name", "user.country"); // hard coded here but (usually defined in a properties file)
Map<String, String> systemMap = System.getProperties()
.entrySet().stream()
.filter(s -> includes != null && includes.contains(s.getKey()))
.sorted(Comparator.comparing(
Map.Entry::getKey, Comparator.comparing(e -> (String) e))
)
.collect(Collectors.toMap(
e -> (String) e.getKey(),
e -> (String) e.getValue(),
(e1, e2) -> e2,
LinkedHashMap::new)
);
I'm keen to know if the code can be tidied up.