Java 8 - Stream.collect() Example

The collect() method of Stream class can be used to accumulate elements of any Stream into a Collection. In Java 8, you will often write code which converts a Collection e.g. List or Set to Stream and then applies some logic using functional programming methods like the filter, map, flatMap and then converts the result back to the Collection e.g. List, Set or Map. In this last part, the collect() method of Stream helps. It accepts a Collector to accumulate elements of Stream into specified Collection. The Collector class provides different methods e.g. toList(), toSet(), toMap(), and toConcurrentMap() to collect the result of Stream into List, Set, Map, and ConcurrentMap in Java.

10 ways to learn a New Technology, Programming language, Frameworks, or Libraries

One of the main challenges of being a programmer or software engineer is the constant learning. Yes, that's true. As a programmer or developer, we always need to learn new things, sometimes every year. Thanks to C, Java, Linux, and SQL, we still have some ground but the way these technologies are used changes every year. For example, we have a new JDK 9 release in 2017, even though many Java developers are still learning Java 8. We also have a Spring Framework 5.0 release in 2017, combined with Spring Security 5.0 and Spring Boot 2.0 release, which many of us need to learn in 2018.

Java 8 Optional Example - isPresent(), OrElse() and get()

The Optional class in Java is one of many goodies we have got from the Java 8 release. If you use correctly, Optional can result in clean code and can also help you to avoid NullPointerException which has bothered Java developer from its inception. Even though many of us have used null to indicate the absence of something, the big problem is that if you call a method or access a field on the null object (except static fields), you will get a NullPointerException and your whole program may crash. The bigger problem is to find the faulty code or root cause because NullPointerException only indicates the line when you try to access field or variable from a null object but how does that null is get created on the code is unknown.