Tag: java8

  • How and when to use computeIfPresent() and computeIfAbsent() methods of Map interface in Java?

    Let’s assume you are developing a java app to keep score of goal scores of football players. Every time a player scores a goal , you call a method with the name of the player and the score gets incremented for that player. What data structure would you choose for this? Ideally a map with…

  • How to use Optional keyword to avoid NullPointerException in nested objects in Java?

    Let’s say you have a nested object in your application . And you want to retrieve the value of a field in the deepest nested object. We will see how to do this using pre-Java 8 and post-Java 8 code (using Optional keyword) Lets consider an Employee object, Let’s say the Employee object has an…

  • Future vs Completable Future in Java

    The best way to run asynchronous threads in Java had been to use the Executor Service framework until Java 8. Executor Service returns a Future object which can be used to retrieve the result of an asynchronous thread. But it comes with few drawbacks. Among the many features introduced in Java 8 , one of…

  • How to measure time in a single unit of time in Java?

    Let’s say you want to find out the number of weeks since your birthday , or the number of decades , or the number of hours . And you want to do this in a single line of code. Java 8 allows you to do that. It provides an API named ChronoUnit under java.time.temporal package…

  • How to calculate your next salary date using Java Temporal Adjuster?

    Java 8 came up with a fresh set of packages for dealing with Time and Date. Prior to this dealing with java dates was quite clumsy. One such package is the java.time.temporal package which allows you to adjust a given date to another date based on certain rules like: Given this date , what is…

  • What are the different ways to group a collection in Java?

    Let’s say you have a collection of phones. The phones have the attributes : model name , phone type (Android or Iphone) , cost , color and rating. And you want to group the phones based on whether they are Iphones or Android Phones. You want to find the costliest IPhone and the costliest Android…

  • The different ways to sort Collections in Java

    Let’s say you have an Employee class with attributes like name , age , rating etc. You want to create a number of employees and sort them all by name. How to do this in Java? You make the Employee class implement Comparable interface which has a method compareTo(). The Employee class needs to implement…

  • How to use Method References in Java?

    As explained in previous posts , lamda expressions allow us to pass a functionality as a parameter. We can dynamically choose function implementations and pass it to another method as an argument . What if the functionality we want to pass already exists and we want to pass that ? Use Method References! Let’s consider…

  • How to use Lamda expressions – Part 1- What are lamda expressions?

    Lamda expressions were introducted in Java 8. They were out of the box , Java programmers weren’t used to anything like this before. So getting adapted to it and start using it need some practise. Once you understand lamda expressions you can super charge your programming skills and become a Super Hero. Ok I am…