Wednesday, April 30, 2014

Comparator Interface -- When we want to sort by our own!

As discussed in my previous blog, we got a brief idea about java.lang.Comparable Interface which is used to sort the elements in a natural ordering. Now in this tutorial, we will learn something new which is comparator interface.

This interface is defined under java.util package so that's why this is sometimes called as a utility interface which should be used as a utility for sorting the objects.
--> Comparator interface in java has a method known as public int compare(Object 1,Object 2) which returns a negative integer, zero or positive integer as obejct 1 is less than, equal to or greater than object 2.

Why Comparator?

Now the question comes , why we want comparator interface when we want to sort the objects by primary key attribute we can do it by comparable Interface. den what is the need of Comparator Interface? And the answer lies here:-


Comparable Interface- a natural Ordering mechanism

We all know about comparable and comparator interface in java a bit but when we deep dive into coding and how they are implemented, there comes the major problem.


Comparator Interface is present in java.lang package which is by default inherited in any class when we write in java. This interface when implemented in any class will help us to compare the current object with any other object of the same class and it imposes a total ordering on the objects of the class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.

Accorind to Oracle Docs http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html, the natural ordering for a class is said to be consistent with equals if and only if e1.compareTo(e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class. Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) return false.


Tuesday, April 29, 2014

First Non Repeating Character in a String

We all know that when we go for companies like amazon or British Telecom, they have a very famous question which is asked very frequently is
Find the first non repeating character in a String which has many repeated alphabets.

I went for amazon interview and encountered such question.
The approach to this question will be first we will think the pseudo -code for that, then we will approach for the exact coding.

So when i was asked this question, i firstly had this pseudo code to make it the most optimize solution for this question.

Fibonacci Series with Reading the Number from a file placed in your Local System

One of this question i encountered while giving interview of Oracle, although qualified well, but at times people gets confused with various Stream Classes defined in java.io package. So to make it more easy to understand and to cope up with Question too, i have come up with a technique where people will read the number upto which we want to print the fibonacci series to from a file and then printing the fibonacci series using recursion.


Employee Department One to Many Relationship implemented using hashMap In java

As we all know that When we are preparing for Java Interviews for some big giants including all investment banks, or topmost companies including Amazon,Oracle or Alcatel you will definitely encounter questions from core java and data structures including algorithmic approach of any given problem. To cope up with, i have come up with this blog. When we go for a java interview, recently to GoldMann Sachhs i was able to reach till 6th round and then they put me on halt.