Posts

Showing posts with the label array

Do you know Javascript … Try these puzzle

Do you know Javascript … Try these puzzle Javascript is one the most miss-understood language among developers. Everyday I learn new lesson about it.While going through few articles I thought of following JS language puzzles. Handling Array var days = [ 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' ]; var length = days. length ; console. log ( 'No of days are ' + days. length ); //Opps we missed Sunday, lets add it days[++ length ] = 'Sunday' ; console. log ( 'Final no of days are ' + days. length ); It is obvious that no of days are 7…. is it ? Written with StackEdit . StackEdit is a full-featured, open-source Markdown editor based on PageDown, the Markdown library used by Stack Overflow and the other Stack Exchange sites. ↩ Here is the text of the footnote . ↩

HashMap hashCode collision by example

Image
As we all know Hash is a part of Java Collection framework and stores key-value pairs. HashMap uses hash Code value of key object to locate their possible in under line collection data structure, to be specific it is nothing but array. Hash code value of key object decide index of array where value object get stored. As per hashcode – equals method implementation rules Objects that are equal according to the equals method must return the same hashCode value. & If two objects are not equal according to equals, they are not required to return different hashCode values. As per above statement it is possible that two different object may have same hashcode values, this is called hashcode collision . To overcome this problem concept of bucket has been introduced. All the Value objects whose corresponding Key’s hashcode values are same , they fall under same bucket. Above diagram explains hash code collision. There are three key-value entries are shown , out of which second and thi...

Composite Design Pattern by example

Image
This post explains Composite design pattern with an example. When to use ? – Use Composite pattern when you have treat a collection of objects as they were once one thing. With Composite design pattern it is very easy to simplify operations over collection data structure. It reduces complexity of the code required if you were going to handle collection as special case. How to Use? - The Composite pattern consist of Component, Leaf and Composite Classes as shown below class diagram Component – Component declares the common interface that both the single and composite nodes will implements Leaf – The leaf class represent the singular atomic data type implementing he component interface.     Composite – Composite is an abstract entity which declares common functionality which are needed by concrete implementation for handling collection data. An Example -   Item.java public interface Item {     public double getPrice(); ...

Graph Searching (BFS&DFS) Implementation

Image
Here is the adjacency matrix representation based implementation of Graph with both graph searching techniques . Following graph has been taken as example Adjacency matrix representation is below int array[][] = {{0,1,1,0,0,0},                      {1,0,0,1,1,0},                      {1,0,0,0,0,1},                      {0,1,0,0,0,0},                      {0,1,0,0,0,0},                      {0,0,1,0,0,0}}; Complete Code is as follows package demo.graph;...

Matrix Spiral Traversing

This post gives solution for 2D Matrix Spiral Traversing . package testproject; /** * * @author Yogesh */ public class SpriralTraverse {     public static void main(String[] args){         //Sample Array         char[][] array = {{'1','2','3','4','5'},                           {'L','1','2','3','6'},                           {'K','E','1','4','7'},                           {'J','D','2','5','8'},             ...