LAMBDA EXPRESSION:
Lambda expression is the most advanced features of java and it introduced in java 8.It basically instance of the functional interface(which interface contain only one abstract method).In lambda expression,there is only abstract functions are implemented.functional interfaces are implement only one abstract method but it can contain any number of default and static method. Runnable and Comparable are the example of functional interface.
STREAM API:
A stream is the sequence of processed data source which supports various operations such as filter, map, reduce, find, match etc.
In-stream the aggregate operations and the bulk operations processing convenient and fast.
Before stream API, for a simple operation like comparing with a string with the element of a string array, we must iterate the total array and compare the String with each element of the array and get the result.
In the above operation, the client program handles all the processing algorithm like iterating the array, comparing the String with the element etc so it is also an external iteration.To avoid external iterations and minimal use of code Stream API provide different functionality to take operation on data sources.
STREAM CREATION:
//Initialize a list type of Integer Listlist=Arrays.asList(0,15,32,45,20,55,24,71); //Stream of Integer list Stream stream=list.stream();
//List of type student class ListstudentList = Arrays.asList(new Student(01,"student1",50),new Student(02,"student2",80),new Student(03,"student3",88)); //Create a stream of StudentList Stream studentStream = studentList.stream(); //Create stream of individual object of list Stream objectStream=Stream.of(studentList.get(0)).collect(Collectors.toList()));
To create a stream of the collection we use the stream() of java.util.Collection interface.
STREAM API OPERATIONS:
Java Stream API provide different operations on collections to process data and manipulate the algorithm of processing. These common operations are forEach, map,collect,filter,findFirst and so on.let's discuss some important operations.
1.Collect:
Collect is use to collect the process result from stream.
public static void streamCollect() { ListstudentList=Arrays.asList(new Student(01,"student1",100),new Student(02,"student2",200),new Student(03,"student3",100)); List list=studentList.stream().filter(n->n.getMarks()==100).map(n->n.getName()).collect(Collectors.toList()); System.out.println("Filtered List who secured 100 mark:"+list); }
2.For Each:
forEach is use to iterate the collections and make operation on the elements of the collections.
public static void streamForEachOperation() { //List of integer type Listlist=Arrays.asList(1,2,3,4,5,6,7,8,9); //initialize new list to store updated value List updateList=new ArrayList (); //forEach operation using stream list.stream().forEach(n->{ updateList.add(n+10); }); System.out.println("After addition of 10,result list:"+updateList); }
3.Map:
Using map in a minimal use of code we can process the element.
public static void streamMapOperation() { //List of integer type Listlist=Arrays.asList(1,2,3,4,5,6,7,8,9); //update value using map List updateList=list.stream().map(n->n+10).collect(Collectors.toList()); System.out.println("After addition of 10 using map,result list:"+updateList); }
This produces a new stream that contains elements of the original stream that pass a given test (specified by a Predicate).
public static void streamFilter() { ListstudentList=Arrays.asList(new Student(01,"student1",100),new Student(02,"student2",200),new Student(03,"student3",100)); List list=studentList.stream().filter(n->n.getMarks()==100).map(n->n.getName()).collect(Collectors.toList()); System.out.println("Filtered List who secured 100 mark:"+list); }
distinct() does not take any argument and returns the distinct elements in the stream, eliminating duplicates. It uses the equals() method of the elements to decide whether two elements are equal or not.
public static void streamDistinct() { Listlist=Arrays.asList(2,5,3,4,2,1,8,1,6,3,5,4); List distinctIntList = list.stream().distinct().collect(Collectors.toList()); System.out.println("Distinct Array:"+distinctIntList); }
Sorted() is Intermediate operations which return a stream of element with natural sequence of order. We can sort with manual order instead of natural order by using Comparator.
public static void streamSorted() { Listlist= Arrays.asList("yogi","prem","ashish","sudhansu","akash","bijoy"); List sortedListAsc = list.stream().sorted().collect(Collectors.toList()); List sortedListDesc = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()); System.out.println("Sorted resultList asc:::"+sortedListAsc); System.out.println("Sort result list desc:::"+sortedListDesc); }
Match work with collections, used to be check the predicate match with the stream. It's return Boolean value in result. Above methods are the some example of match operations.
public static void streamMatch() { Listlist = Arrays.asList("Ashish","Prem","Akash","Bijoy","Raman"); boolean anymatch = list.stream().anyMatch(l -> l.startsWith("A")); boolean allmatch = list.stream().allMatch(l -> l.startsWith("A")); boolean noneMatch = list.stream().noneMatch(l -> l.startsWith("A")); System.out.println("Any match:"+anymatch); System.out.println("All Match:"+allmatch); System.out.println("None Match:"+noneMatch); }
Java count() allow to return count the filtered data. This is a terminal operation which return number of element filtered.
public static void streamCount() { Listlist = Arrays.asList("Ashish","Prem","Akash","Bijoy","Raman"); long count = list.stream().filter(n -> n.startsWith("A")).count(); System.out.println("Count:"+count); }
Reduce operation involves adding elements to a collection, then every time your accumulator function processes an element, it creates a new collection that includes the element, which is inefficient. Reduce() would be more efficient to update an existing collection.
public static void streamReduce() { Listlist = Arrays.asList("Ashish","Prem","Akash","Bijoy","Raman"); String result= list.stream().reduce((m,n) -> m+"-"+n).get(); System.out.println("Reduce string:"+result); }
Comments
Post a Comment