Posts

Showing posts from October, 2019

How to find the day of the week represented by a particular date

In this article, I will be demonstrating how you can find the day of the week represented by a particular date. You can use Java 8 LocalDate class for this. The following code demonstrates this: LocalDate date = LocalDate.now();DayOfWeek dayOfWeek = LocalDate.of(2019, 8, 11);System.out.println("dayOfWeek="+dayOfWeek); The LocalDate class has a method called getDayOfWeek. This returns a DayOfWeek enum which corresponds to the day of the week.  Here a LocalDate object corresponding to 11th August 2019 is created which corresponds to a Sunday. So when you execute this code, it prints the following output: dayOfWeek=SUNDAY

Java 8 BinaryOperator Example

In this blog post, I will be explaining how the Java 8 BinaryOperator functional interface works. To know more about functional interfaces, you can refer to  this  blog post. Edit What is BinaryOperator The BinaryOperator is a specialization of the BiFunction interface that accepts 2 arguments. However, unlike the BiFunction it accepts 2 arguments of the same data type and returns a result of the same data type. The BinaryOperator internally uses the BiFunction.apply method. BinaryOperator Interface Example with Integer data type Consider the following code snippet: BinaryOperator<Integer> additionCalculator = (a, b) -> a +b;int input1 = 6;int input2 = 5;System.out.println("Result of adding " + input1 + " and " + input2 + " is " + additionCalculator.apply(input1, input2));   Here, we have written a BinaryOperator implementation that operates on Integer data type. It accepts two integer values, adds them and returns the result. The BiFu

JAX-RS vs Spring REST - Differences

In this article, I will be covering the differences between JAX-RS/Jersey and Spring REST. What is REST? REST stands for Representational State Transfer.  It is an architectural style that can be used for web services. A REST server exposes functionality as REST endpoints which are simply URLs. A REST client simply uses the services exposed by the REST server by accessing the corresponding URL. Edit What is JAX-RS JAX-RS stands for Java API for RESTful Web Services (JAX-RS). It is the standard Java API for developing RESTful web services. The latest version of JAX-RS is JAX-RS 2.0.  It is part of Java EE 7, so it does not need to be included separately. It is just a specification, it does not provide an implementation. Jersey is the reference implementation of JAX-RS specification. There are other implementations of JAX-RS like RESTEasy, etc   What is Spring REST The Spring framework also provides REST support. The Spring MVC module can be used to develop a REST service. It

Java 8 Map compute methods with code samples

In this article, I will be covering the Java 8 Map compute methods.Java 8 has introduced the Map.compute, Map.computeIfPresent example, and Map.computeIfAbsent methods. Edit compute The compute method computes a new value for a particular key and updates the Map with the new value. Even if the specified key is not present in the map, it computes the value for the key and adds the key-value mapping to the Map . The following code demonstrates this: Map <Integer,String> students = new HashMap<Integer,String>();students.put(100, "Swati Shashtri");students.put(101, "Gina George");students.put(102, "Puja Khanna"); students.compute(100, (key,value) -> value+",Pune");System.out.println(students.get(100));students.compute(101, (key,value) -> value+",Bangalore");System.out.println(students.get(101));   Here, there is a Map that has a student id as the key and student name as the value. There are 3 entries in the Map

Spring Exception Handling With Examples

Spring  In this article, I will be covering the various exception handling mechanisms in Spring . Using an ExceptionHandler The first approach is the ExceptionHandler approach. In this approach, you can designate a method in your controller with the @ExceptionHandler annotation. You can specify some exceptions with this annotation. When those exceptions occur, the code in the method will be executed. The following code demonstrates this: @Controllerpublic class PersonController {//controller methods@ExceptionHandler(RuntimeException.class)public void onRunTimeException(RuntimeException e){//exception handling code for RuntimeException}} Edit Here, the code specifies the @ExceptionHandler annotation on the onRuntimeException class. So this method is designated as an exception handling method. Whenever a RuntimeException occurs within any method in the PersonController, the code invokes this method. You can specify any type and any number of arguments to a method that has the