Java8 Introduction

Dear friends,welcome 2 Java 8. It released by taking some new amazing features. We got lots of updates from java1.1 to 1.7 like collections(1.2) , memory management stuffs etc.but in java8.0 we will get something new. Basically we will get three important feature that are :

  • Functional programming
  • Lambda Expressions
  • Stream API

 

Now we will start with a small update like method definition in interface. We know in 1.7 method is declared in interface but not defined. So here in 1.8 we will see how a method is defining in interface. Second update that we will know that is functional programming. Here we will not focus how to do thing, just focus on what to do thing.Third update is Lambda expression. It is very useful for android programmer and also those people who are working in big data. Basically lambda expressions remove the boiler plate code present when we write anonymous inner classes and here we have to focus main thing. Fourth update is stream API.

Java 8 Stream API Features : forEach() method

Now I am presenting a simple demo program that I have a List containing some integer objects and we want to retrieve, then there are different 4 approaches that I have implemented: among them first three approaches belongs to java 7 and fourth approach belongs to Java 8 feature.

Approach-1:

Package java8s;
import java.util.*;
public class Java8Demo
{
public static void main(String[] args)
{
List l=Arrays.asList(10,20,30,40,50);
for(int i=0;i<5;i++)
{
System.out.println(l.get(i));
}
}
}

Output

10
20
30
40
50

Approach-2:

Package java8s;
import java.util.*;
public class Java8Demo
{
public static void main(String[] args)
{
List l=Arrays.asList(10,20,30,40,50);
//for(int i=0;i<5;i++)
//{
//System.out.println(l.get(i));
//}
Iterator i=l.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}

Output

10
20
30
40
50

Approach-3:

Package java8s;
import java.util.*;
public class Java8Demo
{
public static void main(String[] args)
{
List l=Arrays.asList(10,20,30,40,50);
//for(int i=0;i<5;i++)
//{
//System.out.println(l.get(i));
//}
//Iterator i=l.iterator();
//while(i.hasNext())
//{
//System.out.println(i.next());
//}
for(int i:l)
{
System.out.println(i);
}
}
}

Output

10
20
30
40
50

Approach-4:

Package java8s;
import java.util.*;
public class Java8Demo
{
public static void main(String[] args)
{
List l=Arrays.asList(10,20,30,40,50);
//for(int i=0;i<5;i++)
//{
//System.out.println(l.get(i));
//}
//Iterator i=l.iterator();
//while(i.hasNext())
//{
//System.out.println(i.next());
//}
//for(int i:l)
//{
//System.out.println(i);
//}
l.forEach(i->System.out.println(i)); // java 8 feature
}
}

Output

10
20
30
40
50

I can write this program as:

Package java8s;
import java.util.*;
public class Java8Demo
{
public static void main(String[] args)
{
List l=Arrays.asList(10,20,30,40,50);
l.forEach(i->System.out.println(i)); //java 8 feature
}
}

Output

10
20
30
40
50

 

 

 

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 0674-2361252

Comment