Posts

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