Stream API

Things that can be done using Stream API
1. Print a collection with some filter.
2. Get count of some collection with some filter.
3. Apply multiple filters over a collection.
4. Iterate over a list of array using streams.
5. Apply a logic to each element of collection using some function call and convert it into some new collection.
6. We can find the first element in a ordered collection or can return null.
7. Since forEach is terminal operation instead we can use peek() to return stream and perform operation on that.
8. Short circuiting using skip , limit etc.
9. Sort a Stream using sort() and compartor.
10. Can find min and max with just methods.
11. distinct to find distinct elements.
12. Short circuiting using allMatch, anyMatch and noneMatch()
13. Stream Specialization for primitive data type. IntStreamLongStream, and DoubleStream
14. sum(), average(), range()  to calculate, sum, average and range.

import java.util.Arrays;
import java.util.List;

public class StreamsDemo {
	public static void main(String[] args) {
		List<Persons> people = Arrays.asList(
								new Persons("Tyson","Gill",14),
								new Persons("Martin","Luther",15),
								new Persons("Sam","Watson",16),
								new Persons("Jake","Paul",12),
								new Persons("Duke","Logan",19),
								new Persons("Frost","Freenab",13),
								new Persons("Grin","Jaw",18),
								new Persons("Mak","Simsons",14)
								);
		people.stream()
		.filter(p -> p.getAge()>=15)
		.forEach(p -> System.out.println(p.getfName()));
	}
}
class Persons{
	Persons(String fName, String lName, int age){
		this.fName = fName;
		this.lName = lName;
		this.age = age;
	}
	String fName;
	public String getfName() {
		return fName;
	}
	public void setfName(String fName) {
		this.fName = fName;
	}
	public String getlName() {
		return lName;
	}
	public void setlName(String lName) {
		this.lName = lName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	String lName;
	int age;
}

A Guide to Streams in Java 8: In-Depth Tutorial with Examples

Exception handling using wrapper lamda

Below is the example where you can wrap a lambda inside a lambda.

import java.util.function.BiConsumer;

public class WrapperLambda {
	public static void main(String[] args) {
		int[] inputNumbers = { 1, 2, 3, 4, 5 };
		int key = 0;
		perform(inputNumbers, key, warpperLambda((v, k) -> System.out.println(v / k)));
	}

	private static void perform(int[] inputNumbers, int key, BiConsumer<Integer, Integer> consumer) {
		for (int i : inputNumbers) {
			consumer.accept(i, key);
		}
	}

	private static BiConsumer<Integer, Integer> warpperLambda(BiConsumer<Integer, Integer> consumer) {
		return (v, k) -> {
			try {
				consumer.accept(v, k);
			} catch (Exception e) {
				System.out.println(e);
			}
		};
	}
}

Functional Interfaces

Functional interface are interfaces with only one abstract method in it.
@FunctionalInterface annotation is added so that we can mark an interface as functional interface.

It is not mandatory to add this annotation to our interface but it is best if we do because if some one creates a lambda expression out of the interface and later some one adds method to that interface then those lambda expression will stop working.

@FunctionalInterface
public interface MathDemo {
	public int add(int x,int y);
}

Some of the functional interfaces provided by java 8 so that we don’t create just them manually every time we use lamdas

Instance initializer block

Inside instance block we can perform some extra operations while initializing the instance variable. Of cause we can do initialization inside constructor also, but when we have multiple constructors (overloaded constructors) in our class, and we have certain common operations or initialization in each constructor then we can write those common operations in instance block. Like this way we can make sure that irrespective of any constructor invoked that common operations would be executed or taken care by JVM itself.

This instance block helps to avoid boiler plate code in overloaded constructor

There are mainly three rules for the instance initializer block. They are as follows

  1. The instance initializer block is created when instance of the class is created.
  2. The instance initializer block is invoked after the parent class constructor is invoked (i.e. after super() constructor call).
  3. The instance initializer block comes in the order in which they appear.

Code before compile

public class Demo {
 {
  System.out.println("Hello Instance block1");
 }
 {
  System.out.println("Hello Instance block2");
 }
 Demo(){
  System.out.println("Hello Constructor");
 }
 void display() {
  System.out.println("Hello Display");
 }
 public static void main(String[] args) {
  new Demo().display();
 }
}

Code After Compile(Class file)

public class Demo {
 Demo(){
  super();
  {
   System.out.println("Hello Instance block1");
  }
  {
   System.out.println("Hello Instance block2");
  }
  System.out.println("Hello Constructor");
 }
 void display() {
  System.out.println("Hello Display");
 }
 public static void main(String[] args) {
  new Demo().display();
 }
}

Instance initializer block is useful when there are multiple constructors overloaded in the same class, then we might have to repeat some set of codes in all constructor instead we can make use of initializer block which is automatically called in all the constrcutors.

public class Demo {
 {
  System.out.print("Hello Instance block ");
 }
 Demo(){
  System.out.print("Hello Constructor1 ");
 }
 Demo(String x){
  System.out.print("Hello Constructor2 ");  
 }
 void display() {
  System.out.println("Hello Display ");
 }
 public static void main(String[] args) {
  new Demo().display();
  new Demo("Hello Heapwizard").display();
 }
}

GIT aliases

GIT does not infer to your commands if you type it partially, but if you don’t want to type the entire command with every time you can create aliases for these commands so that you can run these command quickly.

git config --global alias.<alisa_name> <command_with_attibutes>

Some of the alias that i use are below

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci 'commit -v'
$ git config --global alias.st 'status -s'

GIT Branching

How to create new branch in git?

git branch <branch_name>

Note creating new branch only creates new branch it does not switch you to that new branch. You have to do that new branch manually

Check the list of branch present for in the working repository?

git branch
* shows the working branch

You can see which branch of the project is pointing also using the command git log –oneline –decorate

git log --oneline --decorate

Switching branch?

git checkout testing
Note after switch HEAD is pointing to the testing branch

If you want a detailed map of which branch is created from which branch you can run the below command to get the graph of it.

 git log --oneline --decorate --graph --all

You can create a new branch and switch to that new branch in one line using checkout command with -b switch.

git checkout -b <new branch name>

Remote repository in GIT (GIT Remote)

To work in a collaborative project we require GIT repository to be hosted at some location which is accessible to the project technical team. This access can be ready only or read and write depending upon the role you are assigned in the team.

git remote

Git remote is the command to get the shortnames assigned to the repository, using the attribute -v you can check if you have fetch(read) and push(write) access to the repository

When you execute the command git clone <url> then GIT automatically adds the remote of the URL under the short name origin and then executes the command git fetch origin

git clone https://github.com/tyson332/JavaFx

is same as

git remote add origin https://github.com/tyson332/JavaFx
git fetch origin

git remote show <shortname of remote>

You can find the detail information about a particular remote using in the git remote show command.
git remote show :

Above command shows more details about the repository

Amending, unstaging and unmodifying files in GIT

You can make amendments to your comments using the below command

git commit –amend

If you have just committed a file and you want to make some changes to the same commit you can do that using amend in GIT

You can remove the files added to the staging area using the reset command

git reset <filename>

You can remove a file added to the staging area using the reset command.

Unmodifying a file can be done by checking out the file from the repository

git checkout — <file_name>

You can checkout the file from the repository directly using this command, note the changes made to the file will be lost after running this command.

Viewing GIT logs

git log

git log is the GIT command that shows you the commit history of your repository in reverse chronological order(most recent comments shows up first) when executed without any arguments.

Details shown are
1. The SHA-1 checkshum (Example : ca82a6dff817ec66f44342007202690a93763949)
2. Name of the Author name with email id (Example : Scott Chacon schacon@gmail.com)
3. Date of commit (Example : Mon Mar 17 21:52:11 2008 -0700)