Skip to main content


Showing posts from July, 2021

HashMap Internal Implementation in Java

HashMap internal implementation in Java or how does HashMap work internally in Java is a very important interview question. Some of the important points that you should know are- Where does HashMap store its elements internally? What is the term "bucket" in HashMap? What is hashing concept and how does it relate to HashMap? How does put() method work in HashMap? What happens if same hash is calculated for the keys or how are elements stored in case of hash collision? What happens if the null key is added. How does get() method work in HashMap? How does remove() method work in HashMap? In this post we’ll go through the HashMap internal implementation in Java and try to explain the above mentioned points. Note that all the code snippet of the HashMap class provided in this post are from JDK 10. Where does HashMap store its elements Internally HashMap class in Java uses an Array (named table ) of type Node to store its elements. Where N

Default Methods in Java Interface

Java 8 onward a new feature has been added to the interface in Java known as default method in Java interface . With the inclusion of default methods, an interface in Java can have a default implementation for a method. Note that in a normal traditional form of interface all the methods are abstract with no body, now with default method you can even have method implementation with in the interface . Table of contents How is default method defined Why is default method included in interface Java Interface default method example Implementing the interface default method Java default methods - Resolving multiple inheritance issue Extending interfaces having default methods How is default method defined To define an interface default method in Java you need to use default keyword with in the method signature. Interface default method syntax default return_type methodName(arguments) { .. .. } Why is default method included in interface The inclu

Read Excel File in Java Using Apache POI

In this article we’ll see how to read Excel file in Java using Apache POI library. If you have to write to an Excel file in Java you can check this post- Write to Excel File in Java Using Apache POI Table of contents Apache POI Maven dependencies for Apache POI Apache POI classes for reading excel files Read excel file in Java using Apache POI example Apache POI Apache POI is a Java API for Microsoft documents. Using Apache POI you can read and write MS Excel files using Java. In addition, you can read and write MS Word and MS PowerPoint files using Java. Apache POI provides support for reading both OLE2 files and Office Open XML standards (OOXML) files. OLE2 files include most Microsoft Office files such as XLS, DOC, and PPT as well as MFC serialization API based file formats. Office OpenXML Format is the new standards based XML file format found in Microsoft Office 2007 and 2008. This includes XLSX, DOCX and PPTX. Which means Apache POI supports

throw Vs throws in Java Exception Handling

In Java five keywords are used for exception handling namely – try, catch, finally , throw and throws. Out of these five, throw and throws keywords sound quite similar causing confusion thus the question what are the differences between throw and throws in Java exception handling . Throw vs Throws in java 1- throws clause is used to declare an exception with in the method signature where as throw is used with in the code to actually throw an exception. Exception declaration with in the method signature using throws private void readFile() throws IOException { .... .... } Throwing exception using throw keyword try{ if(args.length != 2){ throw new IllegalArgumentException("Two parameters should be passed"); } } 2- With throws clause multiple exceptions can be declared as a comma separated list. Where as throw can throw only a single instance of exception. Declaring multiple exception with throws clause private void readFile() throws FileNotFou

Java throw Keyword With Examples

In the post try-catch block we have already seen some examples of exceptions but all those exceptions are thrown by Java run time. You may choose to throw an exception explicitly in your Java code, that can be done using throw statement in Java . General form of Java throw statement The throw statement requires a single argument; a throwable object. throw throwableObj; Here throwableObj must be an instance of Throwable class or any of its subclass. How to get Throwable class instance If you want to throw an exception explicitly you can get this Throwable class instance in two ways- You can create an instance using new operator. See example . You can use the exception parameter of the catch block and rethrow the exception. See example . If there is a throw statement in a Java code then execution stops as soon as throw statement is encountered. Nearest catch block is checked for the matching exception type, if catch block has the matching exception type then that bl

Spring Boot Microservices Eureka + Ribbon

In the article Spring Boot Microservices example we saw an example of Spring Boot Microservice where we used Eureka for service registration and discovery. In this post we’ll extend that example further to see how to use both Eureka and Ribbon load balancer with Spring Boot to load the balance among micro services instances. In the example there were two separate services User and Account which were registered with Eureka. From User service there was a call to Account service to fetch some data. In this post we’ll see how to configure more than one instances of Account service and use Ribbon load balancer to route the calls among those instances of Account service. Table of contents Load Balancing Using Ribbon load balancing with Eureka Spring Boot Micro Service with Ribbon example Testing the application Load Balancing In simple terms load balancing means distributing the load across several resources rather than putting all the load on a single resource.

Spring Boot Microservices Example

In this article we’ll see a Spring Boot Microservices example with Eureka used for service registration and discovering the service. We’ll have two separate services User and Account developed as Microservices. With two microservices we'll also see how to call one microservice from another using RestTemplate. When a large monolith application is split into two or more microservices those microservices may need to interact with each other. To do that these microservices need to be aware of each others existence and should be able to find each other. This process is known as service discovery . There is a tool called Eureka created by Netflix that can act as a discovery server, for that you need to register your microservices with the Eureka server. So, in this Spring Boot Microservices example we are going to create 3 separate Spring Boot applications two for the functionality of User and Account and third one for Eureka Server. Spring Boot application for Eureka Server

How to Copy a Directory in Java

This post shows how to copy a directory in Java where all the files and sub folders with in a directory are recursively copied to a new directory. Options for copying a directory in Java For copying the folder tree structure which includes the sub-directories and all the files you can use one of the following options in Java- Using File.listFiles() method which returns an array of abstract pathnames denoting the files in the directory. Then you can iterate the array to list the files and copy them to the target directory, you will have to recursively call your method to list files with in the sub-directories. See example . Java 7 onward you can use Files.walkFileTree method which walks a file tree rooted at a given starting file. See example . Java 8 onward you can use Files.walk() method which returns the Path objects as stream by walking the file tree rooted at a given starting file. See example . Directory structure used Java programs shown here to copy a dire