Skip to main content

Java 8 : Streams at Go:



JAVA 8                         



STREAM
INTSTREAM
LONGSTREAM
DOUBLESTREAM
OF
Stream.Of(array)
Stream.of("str1","str2")
LIMIT OPERATION

Stream.Generate()
Stream.iterate()
DISTINCT OPERATION
(list/set).stream.distinct().foreach(x-'>'t;ysout(x);)
(list/set).stream.distinct().collect(toSet());
(list/set).stream.distinct().collect(toList());

CONCAT OPERATION 
Stream.concat(l1.stream(), l2.stream()).collect(Collectors.toList());

FOREACH OPERATION 
(list/set).stream.distinct().foreach()
COUNT OPERATION
(list/set).stream.count()
(list/set).stream.distinct().count()


MATCH OPERATION

(list/set).stream.(anyMatch()|allMatch()|noneMatch())
(list/set).stream.count()
(list/set).stream.distinct().count()
(list/set).stream.(anyMatch()|allMatch()|noneMatch())


FIND OPERATION

(list/set).stream.findAny()
(list/set).stream.findAll()
list.stream().filter(u -> u.getName().endsWith("sh"))
    .findFirst().orElse(null)


FILTER

(list/set).stream.filter(x->x%2).collect(toList()|toSet
list.stream().filter(u -> u.getName().endsWith("sh"))
    .findFirst().orElse(null);
list.stream().filter(u -> u.getName().endsWith("sh"))
    .forEach(u -> System.out.println(u.getName()));
FLATMAP


SORT

NATURAL ORDER :list.stream().sorted() 
REVERSE ORDER : list.stream().sorted(Comparator.reverseOrder()) 
BASED OF A PARTICULAR ATTRIBUTE
list.stream().sorted(Comparator.comparing(Student::getAge))
BASED OF A PARTICULAR ATTRIBUTE REVERSE ORDER


list.stream().sorted(Comparator.comparing(Student::getAge).reversed())
list.stream().max(Comparator.comparing(String::valueOf)).get();
list.stream().min(Comparator.comparing(String::valueOf)).get();
list.stream().sorted(Comparator.reverseOrder())
Functional Programming : 
Pure Functions    -----> Mathematical Functions
1)  Just do one thing 
2) Depends only on their input 
3)  Always return same result for the arguments

Predicate :   T  -> Boolean   

Function .   ( )--> T  

Consumer .  (  ( )--> T  )

Supplier     (  ( )--> T  )

UnaryOperator (   T-----> T . )

BinaryOperator

COMMIT TO MEMORY


How to overcome the issue of adding a new functionality to the API without breaking the code


  1. Java interface default methods will help us in extending interfaces without having the fear of breaking implementation classes.
  2. Java interface default methods has bridge down the differences between interfaces and abstract classes.
  3. Java 8 interface default methods will help us in avoiding utility classes, such as all the Collections class method can be provided in the interfaces itself.
  4. Java interface default methods will help us in removing base implementation classes, we can provide default implementation and the implementation classes can chose which one to override.
  5. One of the major reason for introducing default methods in interfaces is to enhance the Collections API in Java 8 to support lambda expressions.
  6. If any class in the hierarchy has a method with same signature, then default methods become irrelevant. A default method cannot override a method from java.lang.Object. The reasoning is very simple, it’s because Object is the base class for all the java classes. So even if we have Object class methods defined as default methods in interfaces, it will be useless because Object class method will always be used. That’s why to avoid confusion, we can’t have default methods that are overriding Object class methods.
  7. Java interface default methods are also referred to as Defender Methods or Virtual extension methods.









Comments

Popular posts from this blog

Microservices Design patterns

What are microservices? Microservices - also known as the microservice architecture - is an architectural style that structures an application as a collection of services that are Highly maintainable and testable Loosely coupled Independently deployable Organized around business capabilities Owned by a small team The microservice architecture enables the rapid, frequent and reliable delivery of large, complex applications. It also enables an organization to evolve its technology stack. You are developing a server-side enterprise application. It must support a variety of different clients including desktop browsers, mobile browsers and native mobile applications. The application might also expose an API for 3rd parties to consume. It might also integrate with other applications via either web services or a message broker. The application handles requests (HTTP requests and messages) by executing business logic; accessing a database; exchanging messages with other systems; and returni...

GraphQL

What is GraphQL  API Standard invented & open-sourced by Facebook Alternative to  REST API  enables declarative data fetching  exposes single endpoint & responds to queries How it works?  Why Graphql? Improvises performance by reducing the data that is to be transferred over the internet Variety of different frontend frameworks and platforms on client-side Fast development speed & expectation for rapid feature development Why Graphql is better than REST? Flexibility & efficient  No more over /under fetching of data Over fetching : Under fetching: Insightful analytics  Schema serves as contract between client and server CORE CONCEPTS : SDL :SCHEMA DEFINITION LANGUAGE Writing Data with mutations 3 kinds of mutations creating new data updating existing data deleting existing data

Jackson

<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> jackson-core </artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> jackson-annotations </artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> jackson-databind </artifactId> <version>2.9.6</version> </dependency> CBOR encoded data with Jackson <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-cbor</artifactId> <version>2.9.6</version> </dependency> In order to read and write MessagePack encoded data <dependency> <groupId>org.msgpack</groupId> <artifactId>jackson-dataformat-msgp...