Posts

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   }