Posts

Showing posts from July, 2016

Java restful web services with Spring, jersey, JpaRepository, maven, angularjs and oracle

Image
Create Web Project from Maven:   Open CMD > Type the commend mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} - DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false   example :  mvn archetype:generate -DgroupId=com.Webapp -DartifactId=WebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Hashmap capacity, load factor and fail-fast

Capacity :      Capacity is the   N umber of element HashMap can contain.      initial capacity is the capacity at the time the hash table is created        default initial capacity (16)      capacity increased (2 power n) , where n- number of elements load factor : Defines threshold of HashMap. When re-sizing will occur of HashMap. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. default load factor (.75) fail-fast :

What is the difference between hashmap, hashtable and hashset

Hashmap: Hash table implements Map interface Hash table permits null values and the null key HashMap class is equivalent to Hashtable, except that it is unsynchronized and permits nulls It does not guarantee that the order will remain constant over time constant-time performance for the operations (get, put and remove)   Time complexity - O(1) for  get, put and remove                 (Learn About capacity  ,  load factor  and  fail-fast ) It is not synchronized How to make Hashmap Synchronize                  Map m = Collections.synchronizedMap(new HashMap(...)); Exapmle :                    import java.util.*;                    public class HashMapEx{

Understanding final in java

Java has a final keyword that we can use it in three places. Final variable When you use final with a variable, it creates a constant whose value can’t be changed once it has been initialized. Similarly final can be used to  create final methods and final classes. For Example: final int i=10; final File f=new File(); final ClassName; Final methods A final method is a method that can’t be overridden by a subclass. To  create a final method, you simply add the keyword final to the method declaration. For example: public class SpaceShip { public final int getVelocity() { return this.velocity; } }

Merge Sort Java Program

import java.util.Scanner;   public class MergeSort  {          public static void sort(int[] a, int low, int high)      {         int N = high - low;                  if (N <= 1)              return;          int mid = low + N/2;          // recursively sort          sort(a, low, mid);          sort(a, mid, high);          // merging two sorted sub-arrays         int[] temp = new int[N];         int i = low, j = mid;         for (int k = 0; k < N; k++)          {             if (i == mid)                   temp[k] = a[j++]; ...

write a program to find sum of integers from a string

import java.util.regex.Matcher; import java.util.regex.Pattern; public class GetSum { public static void main(String[] args) { String s = " 1ima7 j1 1hh1  0ttr1dfg10 "; Pattern p = Pattern.compile("[0-9]"); Matcher m = p.matcher(s); int sum = 0; while (m.find()) { sum += Integer.parseInt(m.group()); } System.out.println(sum); }

Inheritance in java

Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. Benefits of inheritance reuse of methods  reuse of fields of parent class  We can add new methods and fields Syntax public class className extends ParentclassName   {      //methods and fields   }  

Polymorphism in Java

Polymorphism (from the Greek meaning "having multiple forms") Polymorphism means one thing in many form. an object to have more than one form. Polymorphism is capability of one object to behave in multiple ways. There are following types of polymorphism : Static polymorphism (compile time) function overloading :-      Method Overloading is a feature that allows more than two methods having same name in a class with different parameters. Example: public class Calculator{     void add(int a,int b)   {   System.out.println(a+b);   }     void add(int a,int b,int c)   {   System.out.println(a+b+c);   }     

How to convert .class file to .java file

Image
Step 1: Download Jad Link here JAD  (UnZip it) Step 2: Set environment variable.            

Data abstraction

Abstraction is the process is used to hide certain details and only show the essential features of the object. It is class level design that hiding the complexity of the implementation and offered by an API / design / system, in a sense simplifying the 'interface' to access the underlying implementation. Example: public class Employee {    private String name;    private String address;    private int sal;    public Employee(String name, String address, int sal)    {       this.name = name;       this.address = address;       this.sal = sal;    }

Java Custom Exceptions

Image
Creating Custom Exceptions Here we are going to learn how to create your own exception. The Throwable hierarchy As you know, you use the try/catch statement to catch exceptions, and the throw statement to throw exceptions. Each type of exception that can be caught or thrown is represented by a different exception class. What you might not have realized is that those exception classes use a fairly complex inheritance chain, as shown in Figure :

Java Encapsulation

Encapsulation        Encapsulation is a mechanism of binding the data and function into a single unit. Encapsulation essentially has both information hiding and implementation hiding. Encapsulation provides a way for abstraction. Example: class Encapsulation {     //Restrict direct access, hiding Data     private int a;     private int b;          public void setA(int a){      this.a=a;    }      public void setB(int b){         this.b=b;    }     //hiding implementation     public int sum(){         int total=a+b;         //  Other Implementation         return total;     } }

Java Class concept

Class        A class is a blueprint or prototype from which objects are created. Class that contains data type, Object, function methods, properties, Classes and constructor. Example:               For example our college, Our college contain different different classes and classes contain student. Programming Language Example: class MyClass{     String name= "";     String address= "";     int age= 0;     void setName(String name) {          this.name = name;     }     void setAddress(String address) {          this.address = address;     }     void setAge(int age) {          this.age = age;     }     void print() {          System.out.println(name +" "+Address+" "+age);     } }

Java Object Concept

 Object        Object is representative of the class and is responsible for memory allocation of its data members and member functions.An object is a real world entity having attributes (State / data type) and behaviors (functions). An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object behavior. Example:               In our class room, All students are an individual Object. They have different               name and color. Programming Language Example:  All are the Different different object int a=10; float b1=20.10f; String s1="Name"; MyClass x;

JavaScript Data Types

 JavaScript Data Types Following are the javascript data type :             String, Number, Boolean, Array, Object. Example: Number var length = 10;             String                                       var Name = "Soma";     Array                                     var country = ["India", "China", "USA"];       Object / JSON    var name = {firstName:"Nikita", lastName:"Camon"};  

Object oriented programming concepts (OOPs)

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects". Re-usability is the main concept of Oops. There are few major principles of object-oriented paradigm. If any programming Language supports this principles that programming language called Object-oriented programming. Below are object oriented programming concepts : 1. Object        Read More about Object     http://theprogrammersfirst.blogspot.in/2016/07/java-object-concept.html

Java pagination Using webservices, Spring, JPARepository, jersey, Oracle and Angularjs Grid

Dijkstra's algorithm : graph Shortest Path implementation using java

Image
Input Format The first line contains T, denoting the number of test cases. First line of each test case has two integers N, denoting the number of nodes in the graph and M, denoting the number of edges in the graph. The next M  lines each consist of three space-separated integers x,y,r , where x and y denote the two nodes between which the undirected edge exists, r denotes the length of edge between these corresponding nodes. The last line has an integer , S denoting the starting position. Example: 1 4 4 1 2 24 1 4 20 3 1 3 4 3 12 1

How to change jdk version in intellij

Image
Open Iintellij Goto >>   File > Project Structure

Binary Search Tree : Lowest Common Ancestor

 /* Node is defined as :  class Node      int data;     Node left;     Node right;          */ static Node lca(Node root,int v1,int v2)     {        //Decide if you have to call rekursively     //Samller than both     if(root.data < v1 && root.data < v2){         return lca(root.right,v1,v2);     }     //Bigger than both     if(root.data > v1 && root.data > v2){         return lca(root.left,v1,v2);     }

Swapping Nodes algorithm Implementation using java (binary tree)

Image
Swapping subtrees of a node means that if initially node has left subtree L and right subtree R, then after swapping left subtree will be R and right subtree L. import java.io.*; import java.util.*; public class Solution {