vulab | Open Source IT Training, Java, JEE, Spring Framework, Hibernate, JPA, Big Data, JEE, Hadoop, Spring, Hibernate, Android, IOS, Cloudera, Hortonworks, Big Insights

vulab We provide hands-on training in Java, Big Data, Hadoop, Sqoop, Hive, Impala, Map Reduce, Cloudera, Horton Works, IBM Big Insights, J2ee, Spring Framework, Hibernate, JPA, Android, Apple ios, Java script, HTML5, Database Development, SAP, .NET, Web Development, Quality Assurance, Mobile application development etc....
WE KEEP IT PROFESSIONALS TRAINED AND EMPLOYED.

Posted on 03 February 2015 by Srinivas Nelakuditi

Java Interview Questions

VULAB is the best online provider for advanced Java training. We have successfully trained hundreds of teams and developers in Java. Vulab’s best java online training courses are ready for your enrollment.

Object Oriented Programming (OOP)

Java is a computer programming language that is concurrent, class-based and object-oriented. The advantages of object oriented software development are shown below:

  • Modular development of code, which leads to easy maintenance and modification.
  • Reusability of code.
  • Improved reliability and flexibility of code.
  • Increased understanding of code.

Object-oriented programming contains many significant features, such as encapsulation, inheritance, polymorphism and abstraction. We analyze each feature separately in the following sections.

Encapsulation

Continue Reading ….

0 Comments

Posted on 03 February 2015 by Srinivas Nelakuditi

Apache Spark Introduction from Vulab Hadoop Novice to Professional Training

Why Apache Spark?

To understand why Apache Spark, let us listen to the story of YouTube.

According to You Tube, as of Dec 1st 2014. 300 hours of video is getting uploaded every minute.

If YOUTUBE is built with hardware and software from Oracle compared with OpenSource Big Data Solutions:

  • users will upload about 300 hours of video a minute and 6 billion videos a day along with roughly 100 petabytes of viewed videos a day—would require at least 25 full-rack Exadata machines at $2.0 million each. There would be at least 50 Exadata machines to handle spikes. Those machines would add up to 30 Exalogic devices to serve data at $1.5 million per system. The software stack under Oracle would include WebLogic middleware, Oracle databases, Exadata optimized storage and Oracle as operating system. The open source comparison included JBoss, Tomcat, MySQL, Hadoop and Red Hat Enterprise Linux as the OS.
  • Hardware +  software + staff from Oracle to build YouTube : 2 Billion Dollars
  • Open Hardware + Open Source software + staff to build YouTube : 0.5 Billion Dollars (savings of 75%)
  • Traditional large scale systems are not built for unstructured data like video, email, and logs. So even a traditional database system is not even suitable for hosting youtube kind of application.
  • We need a new systems which is massively expandable, scaleable, fault tolerant, open, extremely fast, reliable and cheap.

Continue Reading ….

0 Comments

Posted on 26 December 2014 by Jawahar Jasthi

Cutomized object sorting in java using Comparable

There can be a situation where  a  list of objects need  to be sorted a particular way .This can be easily achieved by using comparable interface. The following example shows the usage of comparable interface to sort list of snacks using comparable interface .

package com.vulab.comparable.example;

/**
 * @author jawahar
 *
 */
public class Snack {

	private String name;

	private String flavor;

	private int ratings;

	private long noOfreviews;

	public Snack(String name, String flavor, int ratings, long noOfreviews) {
		super();
		this.name = name;
		this.flavor = flavor;
		this.ratings = ratings;
		this.noOfreviews = noOfreviews;
	}

	public int getRatings() {
		return ratings;
	}

	public void setRatings(int ratings) {
		this.ratings = ratings;
	}

	public long getNoOfreviews() {
		return noOfreviews;
	}

	public void setNoOfreviews(long noOfreviews) {
		this.noOfreviews = noOfreviews;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFlavor() {
		return flavor;
	}

	public void setFlavor(String flavor) {
		this.flavor = flavor;
	}

}

The above object has a constructor which takes in the name of the snack , average ratings , no .of reviews , and flavor of the snack .
Following are few possible sorting scenarios.

1)Sorting based on ratings 
package com.vulab.comparable.example;

/**
 * @author jawahar
 *
 */
public class Snack implements Comparable<Snack> {

	private String name;

	private String flavor;

	private int ratings;

	private long noOfreviews;

	public Snack(String name, String flavor, int ratings, long noOfreviews) {
		super();
		this.name = name;
		this.flavor = flavor;
		this.ratings = ratings;
		this.noOfreviews = noOfreviews;
	}

	public int getRatings() {
		return ratings;
	}

	public void setRatings(int ratings) {
		this.ratings = ratings;
	}

	public long getNoOfreviews() {
		return noOfreviews;
	}

	public void setNoOfreviews(long noOfreviews) {
		this.noOfreviews = noOfreviews;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFlavor() {
		return flavor;
	}

	public void setFlavor(String flavor) {
		this.flavor = flavor;
	}


	/* (non-Javadoc)
	 * Sort based on the userRatings
	 */
	@Override
	public int compareTo(Snack snack) {
		int comparUserRatings = snack.getRatings();
		return this.ratings-comparUserRatings;
	}
}

Customized sorting can be implemented using Comparable interface.When the comparable interface is implemented then the object is ready to be sorted when it is a part of a list with other similar object (Snack object list) . Snacks in the list are compared and then an int value is returned  which can be used to assert the position of the Snack in the list of Snacks .
This can be tricky to understand so just imagine that an instance of snack object will compare itself with the next object(say snack1) instance in the list to find the correct position for itself in the list .So when an int value of +ve value is returned then snack object will retain its position else it will find another object to compare itself to untill it gets a -ve value . When a -ve value is reached then the object(snack) has found the right position in the list .

So here is the test class which will be used for sorting the list .

 

package com.vulab.comparable.example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortTest {
	
	public  List<Snack> getList(){
		
	   Snack snack1=new Snack("Tres Leches","sweet",5,1000L);
	   Snack snack2=new Snack("Plum cake","sweet",3,3000L);
	   Snack snack3=new Snack("Chips","Spicy",2,500L);
	   Snack snack4=new Snack("Choclate","Sweet",1,800L);
	   Snack snack5=new Snack("Apple Sauce","Sweet",4,100L);
	   Snack snack6=new Snack("Mango Pudding","Sweet",4,100L);
	   
	   List<Snack> snackList= new ArrayList<Snack>();
	   snackList.add(snack1);
	   snackList.add(snack2);
	   snackList.add(snack3);
	   snackList.add(snack4);
	   snackList.add(snack5);
	   snackList.add(snack6);
	   
		return snackList;
	}
	
	public static void main(String [] args){
		SortTest sort=new SortTest();
		List<Snack> snackList = sort.getList();
		  Collections.sort(snackList);

		for(Snack snack:snackList){
			System.out.println("Snack "+snack.getName());
		}
	}

}

In the above test class Collections.sort(snackList) statement will trigger the sorting process.In this case sorting is based on ratings Console output :

Choclate
Chips
Plum cake
Apple Sauce
Mango Pudding
Tres Leches

2)Sorting based on Name .

If there needs to be a sorting based on name then compartTo method in Snack object can be replaced with the following .

/* (non-Javadoc)
	 * Sort based on the name
	 */
	@Override
	public int compareTo(Snacks snack) {
		return this.name.compareTo(snack.getName());
	}
}

Console output:

Apple Sauce
Chips
Choclate
Mango Pudding
Plum cake
Tres Leches

3)Customized name based sorting – one particular object should always be on the top .

If the use case is to have one particular object to be always on the top then it can be excluded from sorting .

eg: Mango Pudding should always be on the top and rest of the snacks need to be sorted based on the name .

@Override
	public int compareTo(Snack snack) {
		if("Mango Pudding".equalsIgnoreCase(this.getName())){
			return -1;
		}	
	return this.name.compareTo(snack.getName());	
	}

Console output:

Mango Pudding
Apple Sauce
Chips
Choclate
Plum cake
Tres Leches

Name base sorting will return Apple Sauce on the top but since we customized our sorting  “Mango Pudding” will always be on the top .

0 Comments

Posted on 11 December 2014 by Jawahar Jasthi

Custom annotations in java

Annotations construct was introduced as a part of Java SE 5.Custom annotations are rarely used at enterprise level .

By definition an annotation is a metadata that can be added to Java source code. Annotations can be added for  classes, methods, variables, parameters and packages.

In general annotations are tags that can be used by a utility class for processing the code based on the requirement.The processing can be runtime(RetentionPolicy.RUNTIME) or compile time  (RetentionPolicy.SOURCE).

Declaring the custom annotation:

package org.vulab.com.custom.annotation;

 

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

 

@Retention(RetentionPolicy.RUNTIME)

@Target({ElementType.FIELD})

@interface RemoveSpaces {

String getStringValue() default “”;

}

@interface is the syntax used to declare an annotation.

In this example the annotation has only one statement with the definition of a method , return type (String)for the method and the default value as “”(empty string).

@Target defines at what level the annotation is applied which is instance variable in this example.

Other possible values for ElementType are as follows.

  • TYPE – Applied only to Type. A Type can be a Java class or interface or an Enum or even an Annotation.
  • FIELD – Applied only to Java Fields (Objects, Instance or Static, declared at class level).
  • METHOD – Applied only to methods.
  • PARAMETER – Applied only to method parameters in a method definition.
  • CONSTRUCTOR – Can be applicable only to a constructor of a class.
  • LOCAL_VARIABLE – Can be applicable only to Local variables. (Variables that are declared within a method or a block of code).
  • ANNOTATION_TYPE – Applied only to Annotation Types.
  • PACKAGE – Applicable only to a Package.

@Retention

RUNTIME – Annotations are to be recorded in the class file by the compiler and retained by the JVM at run time, so they may be read reflectively for other possible values

SOURCE – Annotations are to be discarded by the compiler .

CLASS – Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time. This is the default behavior.

Below is the utility class that has the logic for the custom annotion we declared earlier.

package com.vulab.custom.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * Logic to Remove spaces from a string
 * 
 */
public class RemoveSpacesUtility {

public static String processCustomAnnotations(Object obj) {
		Class classBeingTested = obj.getClass();

for (Field field : classBeingTested.getDeclaredFields()) {
	try {
		for (Annotation annotation : field.getAnnotations()) {
			if (annotation.annotationType().equals(RemoveSpaces.class)) {
			// This will enables the use private fields of the class
		field.setAccessible(true);
		return removeSpaces(field.get(obj));
		}
	 }
	 }catch (Exception ex) {
		ex.printStackTrace();
			}
		}
		return null;
	}

	private static String removeSpaces(Object object) {
		String string = (String) object;
		// remove all the white spaces from a string
		return string.replaceAll(" ", "");
	}

}

 

Below is the test class for testing the custom annotation

package com.vulab.custom.annotation;

public class StringManipulationTest {

	@RemoveSpaces
	private String name = "Hello World";

	public StringManipulationTest() {
		RemoveSpacesUtility removeSpaces = new RemoveSpacesUtility();
		System.out.println("After removing space "
				+ removeSpaces.processCustomAnnotations(this));
	}

	public static void main(String[] args) {

		StringManipulationTest stringManipulation = new StringManipulationTest();
	}

}

Output :

After removing space HelloWorld

The  test class StringManipulationTest uses the utility class

RemoveSpacesUtility

which has the logic to remove white spaces to  give an output of HelloWorld by taking in “Hello World” as input(with a space).

This is a very simple example to demonstrate how  custom annotation can be used .But there can be many more interesting uses like :

  • Customized Null checking
  • Checking possible error conditions
  • Validation for length of string
  • Range of a Integer value
  • Logging
  • Loading property files  etc.

 

0 Comments

Posted on 06 December 2014 by Srinivas Nelakuditi

Apache Hadoop 2.6 is Available Now.

Congrats to Apache Hadoop Community on releasing the Hadoop 2.6 version.

Yarn storage features have been upgraded in this version.

See the Hadoop 2.6.0 Release Notes for details.

Try it out and let us know if you find any bugs or interesting enhancements.

0 Comments

Posted on 06 December 2014 by Jawahar Jasthi

Pagination with Hibernate ORM or JPA

Pagination of search results is a common requirement for many applications .Database pagination allows you to go through a result set in manageable chunks. This is an important feature when a result set is large. Imagine if the user selects the first result of 500 results returned. Essentially 499 results were wasted. This very taxing and memory allocation and performance on the application server. On the other hand, if we only loaded 10 results into memory, we’ve only wasted 9 results from the result set .

This post shows the implementation of simple query based caching solution, using Hibernate.

1)Create a simple project using maven

mvn archetype:generate -DgroupId=com.javacodegeeks.enterprise.hibernate -DartifactId=HibernateMySQLExample -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

 

Continue Reading ….

0 Comments

Posted on 04 November 2014 by Srinivas Nelakuditi

Java Concurrency Multithreading Tutorial -1

Computers can do a lot of things but they cannot automatically do concurrent programming. Let us learn how to do Concurrent Programming with Java. Java concurrency tutorial from vulab is best java training program, join the program if you want to learn from Java Guru and Expert.
We are going to learn by Example.

Question: How to create a Thread in Java?
Answer: Create your own class by extending java.lang.Thread or implementing java.lang.Runnable interface. Override the public void run() method and provide your concurrent business logic.Using a constructor create threads of your class and call start method.

In the constructor you can use code snippet like this.

new Thread(this).start();

 

0 Comments

Posted on 01 November 2014 by Srinivas Nelakuditi

Java Volatile Keyword Learn by Example

The Java volatile keyword is used to mark a Java variable as “being stored in main memory”. This feature is available since JDK 5.

In Simple Language:

If you have CPU 1 running Thread 1 and CPU2 running Thread 2.

If you want a variable to be used by both the threads, if you declare the variable as volatile, instead of writing the variable value to CPU cache it will be directly written to main memory.

In the below example. We are having two threads Updater, Watcher.

Updater is updating a volatile variable and we can see the Watcher thread picks every change made to the volatile variable. Have fun with Volatile.

 

0 Comments

Posted on 31 October 2014 by Srinivas Nelakuditi

Quick Path to Learn Java with Vulab Java Training

If you are reading this blog, I know you are interested in getting trained on Java. Let us get started with the basics. if you want to learn from Java Experts look no further and we at Vulab provide the best java training.
Java is the programming language created by Sun Micro Systems and released in 1996. Sun Micro systems is now part of Oracle Corporation.

  • Make sure you have a laptop or desktop with Windows, Linux, Unix, or Mac available.
  • Buy any book on Java programming language for reference purpose. I suggest have Java Complete Reference book handy.
  • Have an experienced mentor available to answer any questions. Our best suggestion is to find the best instructor and he will be your best mentor to train you on the java programming language.

Continue Reading ….

1 Comment

Posted on 25 October 2014 by Srinivas Nelakuditi

Java 8 Lambda Expressions Tutorial -7

Let us use Java 8 Lambda feature to do a stream sorting, limiting and collecting the output to a list.
We are going to learn by example.

We will create a list of few BankCheckingAccount objects. We are going to use lambdas to sort and limit only two records and we will store the output in another list.

0 Comments