Posts

Showing posts from June, 2020

What is the difference between Iterator and SplitIterator

In this post, I will be explaining the difference between a Iterator and SplitIterator. Edit Iterator SplitIterator Present in Java right from the start Added by Java 8 Allows iterating over the elements of a collection individually Allows iterating over the elements of a collection individually as well as in bulk Can be used only for iterating over a Collection Can be used to iterate over a Stream as well as a Collection Only allows sequential processing Allows both sequential as well as parallel processing  

Java 9 New Features Explained in Detail

In this article, I will be summing up all the new features introduced by Java 9. Modules Modules are basically  groups of packages . Just like you put a set of related classes into a package, you can put a set of related packages into a module. This helps to organize code better and to follow the Single Responsibility Principle (SRP).  You can deploy a module by itself. So modules help to reduce the size of an application. In order to read more about modules, you can refer to this article. You can also see some of my other articles about module internals and how to create modules . Stream Improvements Java 9 has made several improvements on the Stream interface. These are as follows (You can click the article link for an in-depth explanation of each feature): Stream.ofNullable – Helps to create a Stream with a null value. Stream.iterate   – Helps to create a new finite Stream. The Stream is terminated when the specified condition is true Stream.dropWhile – Helps to filter a Stream

Java Queue Interface Methods Explained With Code Samples

One of the lesser known Collection interfaces in the java.util.Queue interface. In this article, I will be covering this interface in detail. What is the Java Queue interface? The java.util.Queue interface extends the java.util.Collection interface. It provides a First In First Out (FIFO) behavior and you can use it to represent a Queue data structure.  It has several implementations in the Collection API. The java.util.LinkedList and java.util.PriorityQueue are the most common implementations Java Queue Interface Operations There are several methods available on the queue interface that help to perform various queue operations. add The add method supports adding an element to the Queue. if for some reason the add method is unable to add the element, it throws an  Exception. Sample Code (Without Exception) Queue<String> daysOfTheWeek = new LinkedList<String>();daysOfTheWeek.add("Monday");daysOfTheWeek.add("Tuesday");daysOfTheWeek.add("Wednesda

Hibernate List and Set Mapping with code samples

Image
In my earlier article, I gave an introduction to Hibernate Collection mapping. In this article, I will be covering how you can use this feature to map a List or Set to a database table. When to use Collection Mapping Sometimes, your entity may have a field which is a Collection of primitive types. You may want to store these values in the database. In such a case, you cannot use a OneToMany association since the target Collection has primitive values. The Collection Mapping feature is useful in such scenarios. @ElementCollection annotation Just to recap my earlier article, Hibernate/JPA supports the @ElementCollection annotation.  You need to specify this annotation on the Collection that you want to persist to the database. Consider the following Student class: @Entitypublic class Student { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; @ElementCollection private List<Integer> marks; //getters and setters} The Studen

Java 8 DoubleToLongFunction Example

In this blog post, I will be explaining how the Java 8 functional interface  DoubleToLongFunction  works. To know more about functional interfaces, you can refer  this  blog post. Edit What is DoubleToLongFunction The DoubleToLongFunction  interface is a specialization of the  Function  interface. While the  Function interface accepts any data type, the DoubleToLongFunction  interface accepts as parameter a double value and returns a result of type long. To see an example of the Function  interface, refer to  this  blog post.   The DoubleToLongFunction  interface provides a method called  applyAsLong . It accepts a single parameter of double data type. It returns a result of long data type. So it basically applies the logic in the applyAsLong method to the input parameter and returns the result. DoubleToLongFunction Example Code The following code snippet demonstrates the DoubleToLongFunction  interface: DoubleToLongFunction longRetriever = input -> Double.valueOf(input).lon

Python Tuples Explained With Code Samples

In my earlier article, I had covered Python Lists.  In this article, I will be covering Python tuples. What is a Tuple Just like a Python list, a tuple is a set of values.  Unlike a List, a tuple is immutable , so you cannot modify it after it is created. Also, the values in a List are enclosed in square brackets , a tuple on the other hand, uses parentheses . Creating a Tuple You can create a tuple by specifying comma separated values directly or within parentheses.  The following code demonstrates this: mytuple1 = (1,"two",2.5,"three")mytuple2 = 1,5,7print(mytuple1)print(mytuple2)   As you can see from the code above, a Python tuple can contain values of different data types. The above code creates a tuple mytuple1 that has values of type Integer, Decimal and String. It also creates mytuple2  using simple comma separated values. So this code prints the following output: (1, 'two', 2.5, 'three')(1, 5, 7) Accessing values in a Tuple You can acces

How to create a new File on the file system

In this article, I will be demonstrating how you can create a new file on the file system. Using File.createNewFile The File class has a method createNewFile . You can use this to create a new file on the fie system. The following code demonstrates this: private static void createNewFile(String filePath) { File file = new File(filepath); try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }   This code creates a file at the specified  filePath. The file path should include the file location as well as file name. The createNewFile method returns a boolean value which indicates whether file creation was successful or not. Using FileWriter.write The java.io.FileWriter has a write method that you can use to write to a file. If the underlying file does not exist, this method automatically creates the file. So you can use this approach when you want to create a file and write some data to it. The follow

How Spring Works under the Hood

Image
In my earlier article , I gave an introduction to Spring. I covered how the Spring Framework helps in wiring together the objects used in an application. In this article, I will be covering how exactly spring achieves this. Introduction Just to recap , the Spring framework consists of the Spring container. The Spring Container is responsible for creating the objects required in an application and providing them to the necessary classes.  It uses Inversion of Control via Dependency Injection to achieve this. Let us now see how exactly this happens. Edit High Level Overview In order for Spring to be able to wire up the application, it requires developers to provide the following: Beans – The objects in an application are known as beans in Spring terminology. These are simple POJO classes. Spring configures objects of these classes Configuration metadata – Information on how to instantiate the beans and wire them together. The Spring container is also known as the Spring Applic