Succinctness of Lambdas
June 30, 2014 Leave a comment
I think this is impressive. Compare it with the earlier way of iterating over the data. I am streaming a list of java.lang.management.GarbageCollectorMXBean‘s, accessing some data from each and creating an ArrayList of GCType objects in one line of code.
public class GarbageCollectionProbe implements Probe{ Logger l = Logger.getLogger("collection.gc"); public List<GCType> getMemoryPool() { connectJMXRemote(9010); List<GCType> pools = ManagementFactory.getGarbageCollectorMXBeans().stream().map(p-> new GCType(p.getName(), p.getCollectionCount(), p.getCollectionTime())) .collect(Collectors.toCollection(() -> new ArrayList<GCType>())); pools.stream().forEach(p-> l.debug(p)); return pools; } }