Category: java

  • How to use Lamda expressions? – Part 2 – Applying Lamda Expressions

    Let’s apply lamda expressions to a specific use case and see how it helps in building better code. Consider this use case: A manager wants to give promotion to an employee. He follows the below process for this : Check if the employee is eligible Evaluate the employee through interview Grant promotion based on the…

  • 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…

  • How to write custom queries in Spring Data at method level?

    Say you want to write a custom JPA query for one of your data requirements. Spring Data provides great abstraction over JPA to perform CRUD operations. You can easily retrieve entity objects from database based on field values using methods like ‘findByName’ , ‘findByNameAndAge’ etc in your interface extending Spring Data repositories. If you need…

  • How to convert LocalDate to Util Date and vice versa in Java?

    Java 8 introduced LocalDate to represent date without the time information . But sometimes we may need to convert LocalDate to the legacy java util Date in our code . This post explains how to convert from LocalDate to java.util.Date and vice versa. The below code converts local date to Util Date: And the below…

  • How to implement Visitor pattern in Java?

    Let’s say you are running a shop. You sell books, phones , shirts and other stuff. You already have a software to manage your stock. You store the original price, selling price , quantity and name of each item in your database. One day you decide to launch a massive discount sales. And you want…

  • How to implement Interpreter pattern in Java?

    Let’s say you are writing code for an application. You have decided to use only plain JDBC to peform database operations. No ORM (Object Relational Mapping tools). And so you write plain queries in your application code. When you run your code , your application starts throwing Query Syntax errors. You go back to the…

  • How to implement Iterator pattern in java?

    Let’s say you run a small theatre. Your theatre is unique , only those who have membership can watch movies in your theatre. Anyone of any age can be a member. You are not just screening latest movies but curated collections tailored for movie lovers. You have a database of all members . There are…

  • How to implement Memento pattern in Java?

    Let’s say you are designing a shirt online using a tool. You have created a design and saved it. And suddenly you realize you want the old design back. You find a menu to revert , you click it and you get back your old design. Let’s get under the hoods and see how this…

  • How to implement Observer pattern in Java?

    Let’s say my blog is gaining popularity. A lot of people want to be updated with my posts. And let’s assume that my current blog platform doesn’t provide this option. So I am writing an application to update my followers. For now I am considering two types of followers: Email Followers WhatsApp Followers Whenever I…

  • How to implement State Pattern in Java?

    Let’s say you run an eCommerce store. You want to develop an application to run your eCommerce. You are specifically concerned about the delivery of order items. When a customer places an order it goes through many different states: Order is received Order is packed Order is shipped Order is in transit Order is delivered…