Lambda expression:
can use variables defined in outer scope.
can use static, instance and local variables.
local variables must be final or effectively final
map() & flatMap()
both are intermediate operations and returns another stream as a result.
map() -
used for data transformation
It's mapper function produces single value for each input value and hence called one to one mapping
flatMap() -
used for transformation and flattering
It's mapper function produces multiple value for each input value and hence called one to many
mapping
converting stream of stream into single stream - flattering
converting a list of customers into list of string - data transformation
and one customer will have only one email - one to one mapping
map() -
1. processess stream of values
2. It does only mapping
3. Mapper function produces single value for each input
4. data transformation from stream to stream
List<String> emails = [Link]().map(i->[Link]()).collect([Link]());
flatMap()-
1. processess stream of stream of values.
2. it does mapping and flattering
3. mapper function produces multiple values for each input
4. data transformation from Stream<Stream> to stream
List<String> phoneNumbers2 = [Link]().flatMap(i->[Link]().stream()).toList();
reduce method:
T reduce(T identity, BinaryOperator<T> accumulator);
Parallel Streams:
Feature of java8 meant for utilizing multiple cores of the processor. Normally, any java code has one
stream of processing and will execute sequentially. Whereas by using parallel streams, we can divide our
code into multiple streams that are executed in parallel on separate cores. The order of execution is not
under control.
CompletableFuture:
new era of asynchronous programming. Using async programming, we can write non blocking code
where concurrently you can run any N number of task in seperate thread without blocking the main
thread. When the task is completed, it notifies the main thread if the task was completed or not.
There are multiple ways to implements async programming like Future, Executor Service, Callback
interface, Thread pools etc.
disadvantages:
1) Future can't be manually completed
2) Multiple Futures can't be chained together
3) We can't combine multiple futures together
4) No proper exception handling
thenApply(Function), thenAccept(Consumer), thenRun(Runnable)
String joining:
StringJoiner class internally uses StringBuilder class. It is present in [Link] package. It joins only strings
not the array or list of strings.
StringJoiner sj = new StringJoiner("-","[","]");
[Link]("abi");
[Link]("rami");
[Link](sj); //[abi-rami]
[Link] method internally uses StringJoiner class. It is used to join strings, list or array of strings only
with delimiter. (not with prefix or suffix)
String s = [Link]("*", listofString);
[Link]() || [Link](delimiter) || with prefix and suffix
java 8 streams
Streams can be defined as sequence of elements from a source which supports data processing
operations. Streams are not data structures. They can't store data. We can't add or remove elements
from the stream. they does not modify the source. they use the source and perform operations and
return the result.
Shortcicuit operations are operations that does not need the while stream to be processed to produce
the result. They allow stream processing to terminate early when certain condition is met. Eg: allMatch
anyMatch noneMatch skip limit findFirst findAny takeWhile dropWhile
Code:
Integer[] arr = [Link]().toArray(Integer[]::new);
String[] strArr = [Link](arr).mapToObj(i->i+"").toArray(String[]::new);
List<Integer> b = [Link](arr);
[Link]().stream().forEach([Link]::println);
[Link]().forEach([Link]::println);
[Link]().forEach([Link]::println);
[Link](listOfBooks);
[Link](listOfBooks);
[Link]().sorted().forEach([Link]::println);
[Link]().sorted([Link]()).forEach([Link]::println);
class MyComparator1 implements Comparator<Book> {
public int compare(Book b1, Book b2) {
return [Link]()-[Link](); //ascending order
return [Link]()-[Link](); //descending order
return [Link]().compareTo([Link]()); //ascending
return [Link]().compareTo([Link]()); //descending
}
}
Map<String, Integer> sortedMap = new TreeMap<>([Link]());
for customized objects:
[Link]().sorted([Link](i->[Link]())).forEach(i-
>[Link]([Link]()+" "+[Link]()));
[Link]().sorted([Link](Book::getPrice)).map(i-
>[Link]()).forEach([Link]::println);
[Link]().stream().sorted([Link]([Link](Book::getPrice)
.reversed())).forEach(n->[Link]([Link]().getName()+" "+[Link]().getPrice()+"
"+[Link]()));
Links:
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]