Thursday, January 25, 2018

Spring Boot features

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Features:

  1. Create stand-alone Spring applications
  2. Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  3. Provide opinionated 'starter' POMs to simplify your Maven configuration
  4. Automatically configure Spring whenever possible
  5. Provide production-ready features such as metrics, health checks and externalized configuration
  6. Absolutely no code generation and no requirement for XML configuration
 The following are Prerequisite of Spring Boot:

Java 1.8
Gradle 2.3+ or Maven 3.0+
Spring Framework 5.0.0.BUILD-SNAPSHOT
An IDE (Spring Tool Suit) is recommended.

Installing Spring Boot:

Spring Boot can be used with “classic” Java development tools or installed as a command line tool. Regardless, you will need Java SDK v1.6 or higher. You should check your current Java installation before you begin:

$ java -version

You can use Spring Boot in the same way as any standard Java library. Simply include the appropriate spring-boot-*.jar files on your classpath. Spring Boot does not require any special tools integration, so you can use any IDE or text editor; and there is nothing special about a Spring Boot application, so you can run and debug as you would any other Java program.

Although you could just copy Spring Boot jars, we generally recommend that you use a build tool that supports dependency management (such as Maven or Gradle).


How to convert String to JSON object in Java

JSON stands for JavaScript Object notation. There are lot of open source libraries in java to convert JSON String to JSON Object. For Example, Jackson and GSON.

The following example shows converting JSON String to JSON Object using Jackson library:

import java.io.IOException;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class JsonToJavaConverter {
private static Logger logger = Logger.getLogger(JsonToJavaConverter.class);
     
     
public static void main(String args[]) throws JsonParseException
                                                    , JsonMappingException, IOException{

JsonToJavaConverter converter = new JsonToJavaConverter();
             
                String json = "{\n" +
                "    \"name\": \"lucky\",\n" +
                "    \"subject\": \"java\",\n" +
                "    \"address\": \"hyderabad"\n}";
             
//converting JSON String to Java object
converter.fromJson(json);
}
     
public Object fromJson(String json) throws JsonParseException
                                                   , JsonMappingException, IOException{
User lucky = new ObjectMapper().readValue(json, User.class);
logger.info("Java Object created from JSON String ");
logger.info("JSON String : " + json);
logger.info("Java Object : " + lucky);
return lucky;
}
     
public static class User{
private String name;
private String Subject;
private String address;
public String getName() {return name;}
public void setName(String name) {
this.name = name;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [name=" + name + ", subject=" + subject + ", address="
                                        + address + "]";
}
}
}



We will need to include Jackson library dependency in our classpath. To use Jackson with Maven:

As I said, You can either use Jackson or Gson to convert JSON String to Java object and in this Java tutorial we have used Jackson library for JSON to Java object conversion. If you are using Maven for dependency management than you can add following dependency in POM.xml :

<dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-xc</artifactId>
      <version>1.9.11</version>
</dependency>

Now convert JSON String to JSON Object using Gson:

We will need to include Gson library dependency in our classpath. To use Gson with Maven:
<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.2</version>
</dependency>

We can use the Gson class to convert a string to JsonObject using Gson library, for example:

@Test
 public void testStringToJsonObjectWithJsonElement() {
String json = "{\"id\":2,\"name\":\"java\",\"author\":\"lucky\"}";
Gson gson = new Gson();
JsonElement element = gson.fromJson(json, JsonElement.class);
JsonObject jsonObject = element.getAsJsonObject();
assertEquals(2, jsonObject.get("id").getAsInt());
assertEquals("Effective Java", jsonObject.get("name").getAsString());
assertEquals("Joshua Bloch", jsonObject.get("author").getAsString());
}


Java 8 Interview Questions

In this post you will learn some frequently asked java 8 interview questions and answers for freshers and experienced as well. 

1. What are the new features added in Java 8?

Ans: Lambda Expressions:

Lambdas are anonymous functions that capture the variables of its surrounding context. They can be used to treat code as data, or, as an example, to be referenced and used as an ordinary object and passed as argument an to methods and constructors.

Functional interfaces: Functional interfaces are plain old Java interfaces with only one abstract method.

Method References: Method references are also Lambda Expressions. When all a Lambda Expression does is to invoke one method or constructor, you can substitute for a much simpler syntax called Method Reference.

Stream API: a special iterator class that allows the processing of data in a declarative way. Streams can also leverage multi-core architectures without you having to write a single line of multi-threaded code. Writing parallel code is hard and error-prone, as we all know.

Default Methods: Interfaces can now contain implementation code, this is called default methods. It allows an Interface to have methods added to them without having to worry about classes who implement this interface.

2.What are main advantages of using Java 8?
Ans:
  • More compact code
  • Less boiler plate code
  • More readable and reusable code
  • More testable code
  • Parallel operations 
3. Can you create your own functional interface?
Yes, you can create your own functional interface. Java can implicitly identify functional interface but you can also annotate it with @FunctionalInterface.
Example:
Create interface named “Display” as below

public interface display {
void display();
default void displayColor()
{
System.out.println("display Color copy");
}
}
now Create main class named “FunctionalIntefaceMain”
class FunctionalIntefaceMain {

public static void main(String[] args)
{
FunctionalIntefaceMain pMain=new FunctionalIntefaceMain();
pMain.displayForm(() -> System.out.println("displayable form"));
}
public void printForm(display d)
{
d.display();
}
}
Output:
displayable form

5. What are method references?

Ans:
When all a lambda expression does is call one single method from an object or a class, or even a constructor, it can be expressed in a much less verbose way called Method Reference. So, method references are a syntax sugar to create lambda expressions that only invoke one method or constructor.

Syntax:
class::methodname

6.What is Optional?How can you use it?
Ans:

Java 8 has introduced new class Called Optional. This class is basically introduced to avoid NullPointerException in java.Optional class encapsulates optional value which is either present or not.It is a wrapper around object and can be use to avoid NullPointerExceptions.

7. What is the difference between Predicate and Function?
Ans:

Both are functional interfaces.
Predicate<T> is single argument function and either it returns true or false.This can be used as the assignment target for a lambda expression or method reference.
Function<T,R> is also single argument function but it returns an Object.Here T denotes type of input to the function and R denotes type of Result.

8.What is the difference between an interface with a default method and an abstract class?

Ans: Java 8 brought the concept of default methods or method implementations in an Interface. So one might ask what is the difference between an interface and an abstract class now.
Abstract classes can have many things that an interface cannot, like constructors and instance variables. Abstract classes, even if they have only one abstract method, can’t be used as functional interfaces, which seems pretty obvious but it is always better to make it clear.

9.What happens if you create an Interface that extends another interface which contains a Default Method?
Ans:
If an interface that contains a default method is extended, there are three options:

  1. The child interface does not mention the default method: The effect is that the default method is inherited;
  2. The child interface redeclares the method: The method then becomes abstract;
  3. The child interface redefines the method: The effect is that the new method overrides the one from the super interface
10.Do we have PermGen in Java 8? Are you aware of MetaSpace?
Ans:
Until Java 7, JVM used an area called PermGen to store classes. It got removed in Java 8 and replaced by MetaSpace.
Major advantage of MetaSpace over permgen:
PermGen was fixed in term of maximum size and can not grow dynamically but Metaspace can grow dynamically and do not have any size constraint.

11. What are Interface Static Methods in Java 8 ? How are they invoked?

Ans: Java 8 added the possibility to create static methods in interfaces. Neither the interfaces that extend an interface with a static method nor the classes that implement it, inherit this static method, it is necessary to invoke them explicitly using the interface name, just like any other static method: NameOfInteface.staticMethod();
A static method can only be invoked directly by the implementation methods (either static or default) from the defining interface.

12. Explain Java 8-Internal vs. External Iteration?
Ans:
External Iterators- This Iterator is also known as active iterator or explicit iterator. For this type of iterator the control over iteration of elements is with the programmer. Which means that the programmer define when and how the next element of iteration is called.
Internal Iterators- This Iterator is also known as passive iterator, implicit iterator or callback iterator. For this type of iterator the control over the iteration of elements lies with the iterator itself. The programmer only tells the iterator "What operation is to be performed on the elements of the collection". Thus the programmer only declares what is to be done and does not manage and control how the iteration of individual elements take place.

13.What is :: (double colon) operator in Java 8?
Ans:
Usually we use lambda expressions to create anonymous methods which return us the desired output. But sometimes lambda expressions do nothing but call an existing method. Because this lambda expression calls an existing method, method reference can be used here instead of Lambda function. Method reference is described using :: (double colon) symbol.




Wednesday, January 24, 2018

How to Become Big Data Engineer

In this post you will understand how to become Big Data Engineer. Data engineering is an excellent opportunity to start a career in data management for people who have only a basic understanding of machine learning, but are also interested in developing databases and managing them. Thus, such work, of course, is more suitable for software engineers, architects and database administrators.

Make sure you understand that Data Scientist and Data Engineer are not the same thing.

A Data Scientist builds models using mathematics, statistics and machine learning to explain and predict complex behavior, and codifies those models into real-world software. A Data Engineer designs and builds data architectures for ingestion, processing, and surfacing data for large-scale data-intensive applications.

Often the Data Scientist and Data Engineer will work together to build an end-to-end solution for companies requiring advanced analytical models that are operationalized at scale. The Data Scientist is interested in large scale architecture only insomuch as it allows the "science to scale." Thus any Big Data project should have a Data Scientist alongside the Data Engineer to ensure that what gets built is analytically sound (no point in engineering a big data architecture that doesn't prepare and process data in a way that supports the specific models built by the Scientist).Data Engineers should understand the core concepts in computer science and should be very well versed in building and designing large scale applications; end-to-end. They should understand the pros and cons of using relational and noSQL databases. They must understand distributed computing and should be able to work with the Data Scientist to help split algorithms effectively to still yield predictive accuracy across a variety of domains. They should know when to push schemas towards the application to allow for "data lake" designs that assist in large scale analysis but still serve domain-specific applications. And they should be very familiar with the core technologies that are used to build these systems.

To become a data engineer, The following technologies you should have skills:
 
 Programming languages (such as Scala, Java, Python, C++ …) but definitely make sure that you also know at least 1 scripting language (such as javascript). Your job is to basically get data from the source and make sure that it lands somewhere, but the getting and putting can go through scripts and/or apps.

Databases and query languages:

SQL is no excessive luxury, invest in it and make sure that you’re at least a bit proficient. As for databases, make sure that you understand how they work, why to use NoSQL alternatives, what the benefits of certain architectures over others, …

soft skills:

Communication is an essential part of your job. You will need to communicate to business as well as the technical teams and make sure that you grasp everything that both teams are trying to communicate.

Basics of big data:  
 Learn about Spark, Hadoop and make sure that you understand the impact of those types of architectures on the traditional RDBMS systems.

Data management:
 Checking the data quality and managing the data appropriately will be one of your tasks; Make sure you know some techniques to make sure that you guarantee that the data is managed in a correct way.


Wednesday, January 17, 2018

Java flatMap with example

In this post you will learn what is flatMap in Java8 and where we used flatMap. flatMap is a intermediate operation,which returns a new stream. Intermediate operations are classified as stateless and stateful operations based on need for sharing the information between elements while processing.

As per the Javadoc......

public <R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)

Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.) This is an intermediate operation.

The following example understand you how to use flatMap in java8.The same example if you write before java 8 then you could write multiple loops. But in Java 8 it could be like this

Example:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class FlatMapExample {
public static void main(String args[]) {
List<Zoo> zooList = new ArrayList<>();
Zoo nationalZoo = new Zoo("National");
nationalZoo.add("Lion");
 nationalZoo.add("Tiger");
 nationalZoo.add("Peacock");
      nationalZoo.add("Gorilla");

        Zoo aCountyZoo = new Zoo("Wills County");
        aCountyZoo.add("Peacock");
        aCountyZoo.add("Camelion");

        zooList.add(nationalZoo);
        zooList.add(aCountyZoo);

        // to get the aggregate
 List<String> animalList = zooList.stream()
                .flatMap(element -> element.getAnimals().stream())
                .collect(Collectors.toList());
System.out.println(animalList);

        // to get the unique set
Set<String> animalSet = zooList.stream()
                .flatMap(element -> element.getAnimals().stream())
                .collect(Collectors.toSet());
System.out.println(animalSet);
}
}
class Zoo {
private String zooName;
private Set<String> animals;
public Zoo(String zooName) {
this.zooName = zooName;
this.animals = new HashSet<>();
}
public void add(String animal) {
this.animals.add(animal);
}
public Set<String> getAnimals() {
return animals;
}
}

Output:
[Peacock, Lion, Tiger, Gorilla, Peacock, Camelion]
[Peacock, Lion, Tiger, Gorilla, Camelion]

Imagine that you want to create the following sequence: 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 etc. (in other words: 1x1, 2x2, 3x3 etc.)

With flatMap it could look like:


IntStream sequence = IntStream.rangeClosed(1, 4)
                          .flatMap(i -> IntStream.iterate(i, identity()).limit(i));
sequence.forEach(System.out::println);

where:

IntStream.rangeClosed(1, 4) creates a stream of int from 1 to 4, inclusive
IntStream.iterate(i, identity()).limit(i) creates a stream of length i of int i - so applied to i = 4 it creates a stream: 4, 4, 4, 4
flatMap "flattens" the stream and "concatenates" it to the original stream

With Java < 8 you would need two nested loops:

List<Integer> list = new ArrayList<>();
for (int i = 1; i <= 4; i++) {
    for (int j = 0; j < i; j++) {
        list.add(i);
    }
}

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...