Posts

Showing posts from 2015

Lionel Messi

" I start early and I stay late, Day after day, year after year, It took me 17 years and 114 days To become an overnight success." - Lionel Messi

What the Heck is the Big O Notation?

Image
The Idiots Guide to Big O I hate big O notation. For as long as I can remember it’s been my biggest achilles heel (of which I have many). It’s just something I’ve never managed to successfully motivate myself to learn about despite knowing it’s going to come up in every single interview. I’ll get asked to implement an algorithm which I’ll do via brute force to start with: “What is the Big O of that?”. “I don’t know Big O but I know it’s slow”. This is the wrong attitude and the wrong approach, and I have finally decided it is time to face my demons. All the resources I’ve found online I’ve sent me quickly to sleep, so I am determined to create an accessible Big O mastery page. What on earth is Big O? Big O is the way of measuring the efficiency of an algorithm and how well it scales based on the size of the dataset.  Imagine you have a list of 10 objects, and you want to sort them in order.  There’s a whole bunch of algorithms you can use to make that happen, but not all

Java XML Parsing Made Easy

Unlike with many other modern languages, parsing XML in Java requires more than one line of code. XML traversing using XPath takes even more code, and I find this is unfair and annoying. I'm a big fan of XML and use it it in almost every Java application. Some time ago, I decided to put all of that XML-to-DOM parsing code into a small library —jcabi-xml. Put simply, the library is a convenient wrapper for JDK-native DOM manipulations. That's why it is small and dependency-free. With the following example, you can see just how simple XML parsing can be: import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; XML xml = new XMLDocument(   "<root><a>hello</a><b>world!</b></root>" ); Now, we have an object of interface XML that can traverse the XML tree and convert it back to text. For example: // outputs "hello" System.out.println(xml.xpath("/root/a/text()").get(0)); // outputs the entire XML d

How SQL Indexes Work internally ?

Image
When an index is created, it records the location of values in a table that are associated with the column that is indexed. Entries are added to the index when new data is added to the table. When a query is executed against the database and a condition is specified on a column in the WHERE clause that is indexed, the index is first searched for the values specified in the WHERE clause. If the value is found in the index, the index returns the exact location of the searched data in the table. Figure 16.1 illustrates how an index functions. Suppose the following query was issued: SELECT *  FROM TABLE_NAME WHERE NAME = 'SMITH'; As shown in Figure 16.1, the NAME index is referenced to resolve the location of all names equal to 'SMITH'. After the location is determined, the data can be quickly retrieved from the table. The data, in this case names, is alphabetized in the index. A full table scan would occur if there were no index on the table and the same que

Java Documentation Tips and Tricks Referral link

A great source to know about Java Documentation. http://zeroturnaround.com/rebellabs/reasons-tips-and-tricks-for-better-java-documentation/?utm_source=mkto&utm_medium=email&utm_campaign=RebelLabsNewsletterApril

How does java Hashmap works internally,,!

What is Hashing? Hashing in its simplest form, is a way to assigning a unique code for any variable/object after applying any formula/algorithm on its properties. A true Hashing function must follow this rule: Hash function should return the same hash code each and every time, when function is applied on same or equal objects. In other words, two equal objects must produce same hash code consistently. Note : All objects in java inherit a default implementation of hashCode() function defined in Object class. This function produce hash code by typically converting the internal address of the object into an integer, thus producing different hash codes for all different objects. HashMap is an array of Entry objects: Consider HashMap as just an array of objects. Have a look what this Object is: static class Entry<K,V> implements Map.Entry<K,V> {           final K key;           V value;           Entry<K,V> next;           final int hash;   ...    }  

Mutable and Immutable Objects

Mutable Objects:  When you have a reference to an instance of an object, the contents of that instance  can  be altered Immutable Objects:  When you have a reference to an instance of an object, the contents of that instance  cannot  be altered Immutability and Instances To demonstrate this behaviour, we'll use  java.lang.String  as the immutable class and  java.awt.Point  as the mutable class. Point myPoint = new Point( 0, 0 ); System.out.println( myPoint ); myPoint.setLocation( 1.0, 0.0 ); System.out.println( myPoint ); String myString = new String( "old String" ); System.out.println( myString ); myString.replaceAll( "old", "new" ); System.out.println( myString ); In case you can't see what the output is, here it is: java.awt.Point[0.0, 0.0] java.awt.Point[1.0, 0.0] old String old String We are only looking at a single instance of each object, but we can see that the contents of  myPoint  has changed, but the contents o

When to go for Interfaces and Abstract Classes?

From Java How to Program about abstract classes: Because they’re used only as superclasses in inheritance hierarchies, we refer to them as abstract superclasses. These classes cannot be used to instantiate objects, because abstract classes are incomplete. Subclasses must declare the “missing pieces” to become “concrete” classes, from which you can instantiate objects. Otherwise, these subclasses, too, will be abstract. To answer your question "What is the reason to use interfacs?": An abstract class’s purpose is to provide an appropriate superclass from which other classes can inherit and thus share a common design. As opposed to an interface: An interface describes a set of methods that can be called on an object, but does not provide concrete implementations for all the methods... Once a class implements an interface, all objects of that class have an is-a relationship with the interface type, and all objects of the class are guaranteed to provide the functionality

Difference between Comparable and Comparator?

Comparable A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances. Comparator A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface. Comparator provides a way for you to provide custom comparison logic for types that you have no control over. Comparable allows you to specify how objects that you are implementing get compared. Obviously, if you don't have control over a class (or you want to provide multiple ways to compare objects that you do have control over) then use Comparator. Otherwise you can use Comparable. Other Sources: http://www.digizol.com/2008/07/java-sorting-comparator-vs-comparable.html http://www.programcreek.com/2011/12/examples-to-demonstrate-comparable-vs-

What are Interfaces and When to use Interfaces?

Interfaces: A good place to look at Java Interfaces would be the collections framework. java.util.List //interface java.util.ArrayList //Concrete class java.util.LinkedList //Concrete class So you can write code like this: List l = new ArrayList(); l.add(..) //do something else. If in future you want to change the implementation with say LinkedList or you own AwesomeList which implements List interface, all you have to do is change the very first line to: List l = new MyAwesomeList(); or List l = new LinkedList(); The rest of the code would follow through. More Examples: Use interfaces to define an application programming contract (blueprint, interface) which "3rd-party" vendors have to fully adhere and implement. This way the endusers can just code against the API contract and easily switch of the concrete implementation "under the hoods" without changing the code. The JDBC API is an excellent example. It exist of almost only interfaces

What is Docker? Why it is popular?

Image
Docker, a new container technology, is hotter than hot because it makes it possible to get far more apps running on the same old servers and it also makes it very easy to package and ship programs. Here's what you need to know about it. All the noise is happening because companies are adopting Docker at a remarkable rate. At OSCon in July, I ran into numerous businesses that were already moving their server applications from virtual machines (VM) to containers. Indeed, James Turnbull, Docker's VP of services and support, told me at the conference that three of the largest banks that had been using Docker in beta were moving it into production. That's a heck of a confident move for any 1.0 technology, but it's almost unheard of in the safety-first financial world. At the same time, Docker, an open-source technology, isn't just the darling of Linux powers such as Red Hat and Canonical. Proprietary software companies such as  Microsoft have also embraced D