Java 8 Streams:
Whereas a java Stream is a data structure that is computed on-demand and do internal iteration.Java Stream doesn’t store data, it operates on the source data structure (collection and array) and produce pipelined data that we can use and perform specific operations. Such as we can create a stream from the list and filter it based on a condition.Java 8 Stream internal iteration principle helps in achieving lazy-seeking in some of the stream operations.Commonly used intermediate operations are
Java Streams are consumable, so there is no way to create a reference to stream for future usage. Since the data is on-demand, it’s not possible to reuse the same stream multiple times.
Functional Interfaces in Stream are :
Function and BiFunction:takes one input returns another.
Object obj=list.stream().filter( i->((Employee)i).getEmpName().startsWith("A")).collect(Collectors.toList());
List list2=(ArrayList) obj;
Lambda Expression:
Method references provide the useful syntax to refer directly to exiting methods or constructors of Java classes or objects (instances). It can be to a Class::new (Constructor),Static method,instance method or to a pair of object referance and instance method.
Eg:cars.forEach( Car::collide );cars.forEach( Car::collide );
Whereas a java Stream is a data structure that is computed on-demand and do internal iteration.Java Stream doesn’t store data, it operates on the source data structure (collection and array) and produce pipelined data that we can use and perform specific operations. Such as we can create a stream from the list and filter it based on a condition.Java 8 Stream internal iteration principle helps in achieving lazy-seeking in some of the stream operations.Commonly used intermediate operations are
filter
and map
.Commonly used terminal methods are forEach
, toArray
, min
, max
, findFirst
, anyMatch
, allMatch
etc. You can identify terminal methods from the return type, they will never return a Stream.Java Streams are consumable, so there is no way to create a reference to stream for future usage. Since the data is on-demand, it’s not possible to reuse the same stream multiple times.
Functional Interfaces in Stream are :
Function and BiFunction:takes one input returns another.
ToIntFunction
, ToLongFunction
, ToDoubleFunction
, ToIntBiFunction
, ToLongBiFunction
, ToDoubleBiFunction
, LongToIntFunction
, LongToDoubleFunction
, IntToLongFunction
, IntToDoubleFunction
Predicate and BiPredicate
Consumer and BiConsumer :consumes a type and do not return any object
Java Optional is a container object which may or may not contain a non-null value. If a value is present,
isPresent()
will return true and get()
will return the value. Stream terminal operations return Optional object. Some of these methods are:
- Optional
reduce(BinaryOperator accumulator) - Optional
min(Comparator comparator) - Optional
max(Comparator comparator) - Optional
findFirst() - Optional
findAny()
Converting Java Stream to Collection or Array
There are several ways through which we can get a Collection or Array from a java Stream.
- We can use java Stream
collect()
method to get List, Map or Set from stream.CopyStreamintStream = Stream.of(1,2,3,4); List intList = intStream.collect(Collectors.toList()); System.out.println(intList); //prints [1, 2, 3, 4] intStream = Stream.of(1,2,3,4); //stream is closed, so we need to create it again Map intMap = intStream.collect(Collectors.toMap(i -> i, i -> i+10)); System.out.println(intMap); //prints {1=11, 2=12, 3=13, 4=14}
Example:
Object obj=list.stream().filter( i->((Employee)i).getEmpName().startsWith("A")).collect(Collectors.toList());
List
Lambda Expression:
Map<String, Integer> items = new HashMap<>();
items.put("A", 10);
items.put("B", 20);
items.put("C", 30);
items.put("D", 40);
items.put("E", 50);
items.put("F", 60);
items.forEach((k,v)->System.out.println("Item : " + k + " Count : " + v));
Method references provide the useful syntax to refer directly to exiting methods or constructors of Java classes or objects (instances). It can be to a Class::new (Constructor),Static method,instance method or to a pair of object referance and instance method.
Eg:cars.forEach( Car::collide );cars.forEach( Car::collide );
No comments:
Post a Comment