Category: java

  • How to convert a Java standalone application into a jar file through command line?

    Let’s say you created a standalone java application and you want to execute it as a jar file. You want to run the main method of a particular class in your application when the jar file is executed. This post explains the steps to convert a java application to a jar file using command line.…

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

  • HTTP Client – Java 11 – Making HTTP requests in Java is so easy now

    Until Java 11 , making HTTP requests was not intuitive . You had to use the non friendly URLConnection class. With Java 11 , it looks easier and cool. To make any HTTP request ,you need the following: A HTTP Request A HTTP response A HTTP client to send the request and get the response…

  • How to represent a composite key in hibernate?

    Consider the below class representing a Student. It has a primary key id , a class id and a name field. Say you want to combine the id and class id into a composite key. You want the same student id to be used against a different class id. We can achieve this in hibernate…

  • 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 implement Concurrency in Java – A very minimalist example

    Let’s say you are implementing a batch service and you want to execute a lot of methods. Let’s assume they are independent functionalities and the output of one is not used by any other methods. Executing them one by one will consume a lot of time. The best strategy is to execute them in parallel.…

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