Difference between HashSet vs TreeSet in Java? [Answer]

HashSet and TreeSet both implement same interface i.e  java.util.Set interface and they possess the quality of Set interface means duplicate elements are not allowed. Both HashSet and TreeSet are used to store unique elements, but HashSet doesn't care about any order and TreeSet keeps a thing in order. Ordering or sorting on TreeSet can be customized by using the Comparator interface, by default TreeSet uses elements of natural order for sorting, which is defined by the compareTo() method of java.lang.Comparable interface.  What is the difference between HashSet and TreeSet is also one of the frequently asked Java interview questions, So you should know about similarities and differences between them? 

What is fail safe and fail fast Iterator in Java?

Java Collections supports two types of Iterator, fail-safe and fail fast. The main distinction between a fail-fast and fail-safe Iterator is whether or not the underlying collection can be modified while it begins iterated. If you have used Collection like ArrayList then you know that when you iterate over them, no other thread should modify the collection. If the Iterator detects any structural change after iteration has begun e.g adding or removing a new element then it throws ConcurrentModificationException,  this is known as fail-fast behavior and these iterators are called fail-fast iterator because they fail as soon as they detect any modification. 

How to calculate Sum of Digits using Recursion in Java [Example]

This is the second part of our article to solve this coding interview question,   how to find the sum of digits of an integer number in Java. In the first part, we have solved this problem without using recursion i.e. by using a while loop and in this part, we will solve it by using recursion. It's good to know different approaches to solving the same problem, this will help you to do well on coding interviews. While finding a recursive algorithm, always search for a base case, which requires special handling. Once you find the base case, you can easily code the method by delegating the rest of the processing to the method itself, i.e. by using recursion.  

How to compare String Objects in Java [Example Tutorial]

The String is a special class in Java, so is String comparison. When I say comparing String variables, it can be either to compare two String objects to check if they are the same, i.e. contains the same characters, or compare them alphabetically to check which comes first or second. In this article, we are going to talk about the right way of comparing String variables, but what is the wrong way? The wrong way is to compare String using the == operator. It is one area in which almost every Java programmer has made mistakes sometimes by comparing two String variables using the == operator. 

Difference between String literal and New String object in Java

The String class or java.lang.String is a special class in Java API and has so many special behaviors which are not obvious to many programmers. In order to master Java, the first step is to master the String class, and one way to explore is checking what kind of String related questions are asked on Java interviews. Apart from usual questions like why String is final or equals vs == operator, one of the most frequently asked questions is what is the difference between String literal and String object in Java

Top 20 Hibernate Interview Questions with Answers for Java Programmers

Hibernate is one of the most popular persistent frameworks in the Java world. Hibernate offers an object to relational (ORM) solution which frees Java developers from writing tedious, hard to read, and cluttered JDBC code converting SQL columns into Object properties. Apart from freeing Java developers from writing JDBC and database interaction code, Hibernate also offers the out-of-box solution on caching, proxying, and lazy loading which drastically improves the performance of your Java Web application. 

Difference between dependency injection and factory pattern?

Difference between dependency injection and factory pattern?
--------------------------------------------------------------
What is difference between factory pattern and dependency injection or difference between sprint IOC and factory pattern? are some of the frequently asked quesiton in Java irvs. Though both factory pattern and dependency injection might look similar, there is a fundamental difference between them. Factory pattern takes responsibility of creating object, while dependency injection inverse the process how an object gets his dependency, it transfer that responsibility to an external party e.g. Spring IOC transfer object creation and management functionality to the IOC container. In other words, If your use Factory pattern then your object has to retrieve dependency by himself, but in case of dependency injection, dependency will be injected to the object by container or whoever manages that object.

Advantages of Dependency Injection over Factory Pattern

1. Better decoupling of classes .

2. Better Unit testing . Now it is easier to inject mock implementation of the services into the object being tested.


You might also like.


In short, though both Factory pattern and DI helps object creation, DI is much better than Factory. With DI you get better decoupling of object and its dependency. Dependency injection also makes unit testing easier, which is very important to keep quality control in check. BTW, nothing comes free, in order to use dependency injection you need a container e.g. Spring IOC, while you can use Factory pattern by your own. You also need to provide configuraiton to initialize object and inject dependency, like one we use with Spring to initialize beans e.g. aplicatoin-Context.xml