Stream with java

Stream with java

?Stream?was one of the major features added to Java 8.

A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.

java stream provide many features to developer like:

  • A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels.
  • Streams don’t change the original data structure, they only provide the result as per the pipelined methods.
  • Each intermediate operation is lazily executed and returns a stream as a result, hence various intermediate operations can be pipelined. Terminal operations mark the end of the stream and return the result.


how i can create stream in java project

for example i have the following code list of some thing object and i want put this list inside stream to print each object inside list


private static Employee[] arrayOfEmps = {
    new Employee(1, "Jeff Bezos", 100000.0), 
    new Employee(2, "Bill Gates", 200000.0), 
    new Employee(3, "Mark Zuckerberg", 300000.0)
};

private static List<Employee> empList = Arrays.asList(arrayOfEmps);

empList.stream().forEach(employee->{ // here i use lambda expression
  system.out.printLn(employee.toString());
  });
 
  
  

          


the above code i will run the code and print all arrayOfEmps by stream API

you can read about lambda expression when click here


now i want say about FUNCTIONAL INTERFACES USED IN JAVA STREAMS

Stream.java?provides different methods to process list elements,?map(),?flatMap(),?filter(),?sorted()?etc, each of which takes a functional interface type as an argument.

in the following code i will provide filtered Name array using lambda expression with stream API

String[] names=["shamk x1","cat x2","look","like"];

// now i will filter name and i need name 1

List<Employee> empList = Arrays.asList(arrayOfEmps);

List<String> filteredName=empList.stream()
      .fliter(name->name.contains("a"))
 // i will filter the name when match when return true
        ).collect(Collectors.toList); // return all match to list

        


now how the filter work inside stream API

the filter structures inside stream interface like following image

No alt text provided for this image

the filter take Predicate as an argument

so what is Predicate?

A predicate is a?functional interface?provided in?java.util.function?package and contains one abstract method, which is,?boolean test(T t).

you can see the following image to understand predicate

No alt text provided for this image

but now i think you don't understand how you can implement test inside functional interface

okay! look at the following code

.fliter(name->name.contains("x"))        

i used this code when write the filter name code and the lambda expression here is the implement of test method inside Predicate interface

you can see the following image to understand filter life cycle inside stream api

No alt text provided for this image


the next article i will explain about other method in stream API ....



要查看或添加评论,请登录

Abd-Alrhman Alkraien的更多文章

  • Base Entity and Audit entity with?JPA

    Base Entity and Audit entity with?JPA

    Introduction: When creating any web application and wanting to connect it with DB, we will add id as a column inside…

    1 条评论
  • Java 8 — Stream APIs

    Java 8 — Stream APIs

    overview When Java 8 came into this world, it comes to provide us with more features, flexibility, and comfortable to…

  • Java with Jackson and JSON

    Java with Jackson and JSON

    Introduction: every developer knows about JSON, and many developers use Json format with API requests or responses. So…

    2 条评论
  • Liquibase Tutorial

    Liquibase Tutorial

    Introduction: Sometimes we need to create a database and put fake data inside it, when running the application, we can…

    1 条评论
  • Element collection Vs One to Many in JPA and hibernate

    Element collection Vs One to Many in JPA and hibernate

    Introduction: Inside any project will have a database for saving and fetching the data from it, and will save the data…

  • Java history from Java 1.0 to Java 18

    Java history from Java 1.0 to Java 18

    Introduction: From this article, you can see the version of java from the first to the latest version. And what are the…

  • Immutability in Java

    Immutability in Java

    What is Immutability?!! An object is considered immutable if its state cannot change after it is constructed. Maximum…

    2 条评论
  • Logging Spring requests using aspect

    Logging Spring requests using aspect

    Introduction: In any web application we need logging request and response, We can do it using Log4j in java, but in…

  • Aspect Object Programing with Spring boot?Example

    Aspect Object Programing with Spring boot?Example

    Introduction: In the previous article, we talked about Aspect and if you read the previous article you should know the…

  • Aspect object programming with Spring?boot

    Aspect object programming with Spring?boot

    Introduction: The programming in the world is changing every day and the projects are bigger in the last few days. so…

    1 条评论

社区洞察

其他会员也浏览了