Monday 28 August 2017

Terminal operations[collect] in Java 8 Stream | Streams in Java 8


Click here to watch in Youtube :
https://www.youtube.com/watch?v=q9K19rkscYM&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Terminal operations[collect] in Java 8 Stream | Streams in Java 8 
Customer.java
public class Customer
{
    private String firstName;
    private String lastName;

    public Customer(String firstName, String lastName)
    {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    @Override
    public String toString()
    {
        return "Customer [firstName=" + firstName + ", lastName="
                + lastName + "]";
    }

}
StreamCollectorDemo1.java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamCollectorDemo1
{
    public static void main(String[] args)
    {
        List<Customer> customerList = Arrays.asList(
                new Customer("Peter", "John"),
                new Customer("Steve", "Smith"),
                new Customer("Dave", "John"),
                new Customer("Robert", "pattinson"));

        List<Customer> filteredList = customerList.stream()
                                                  .filter(c -> c.getLastName().startsWith("John"))
                                                  .collect(Collectors.toList());

        System.out.println(filteredList);

    }
}
Output
[Customer [firstName=Peter, lastName=John], Customer [firstName=Dave, lastName=John]]
StreamCollectorDemo2.java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamCollectorDemo2
{
    public static void main(String[] args)
    {
        List<Customer> customerList = Arrays.asList(
                new Customer("Peter", "John"),
                new Customer("Steve", "Smith"),
                new Customer("Dave", "John"),
                new Customer("Robert", "pattinson"));

        Map<String, List<Customer>> groupMap = customerList.stream()
                                              .collect(Collectors.groupingBy(c -> c.getLastName()));
         
        groupMap.forEach(
                (lastName, customer) -> System.out.println(lastName + ": " + customer));

    }
}
Output
Smith: [Customer [firstName=Steve, lastName=Smith]]
John: [Customer [firstName=Peter, lastName=John], Customer [firstName=Dave, lastName=John]]
pattinson: [Customer [firstName=Robert, lastName=pattinson]]
StreamCollectorDemo3.java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamCollectorDemo3
{
    public static void main(String[] args)
    {
        List<Customer> customerList = Arrays.asList(
                new Customer("Peter", "John"),
                new Customer("Steve", "Smith"),
                new Customer("Dave", "John"),
                new Customer("Robert", "pattinson"));

        Map<String, String> lastNameMap = customerList.stream()
                .collect(Collectors.toMap(
                        c -> c.getLastName(), c -> c
                                .getFirstName(),
                        (firstName1, firstName2) -> firstName1 + ","
                                + firstName2));

        System.out.println(lastNameMap);

    }
}
Output
{Smith=Steve, John=Peter,Dave, pattinson=Robert}
StreamCollectorDemo4.java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamCollectorDemo4
{
    public static void main(String[] args)
    {
        List<Customer> customerList = Arrays.asList(
                new Customer("Peter", "John"),
                new Customer("Steve", "Smith"),
                new Customer("Dave", "John"),
                new Customer("Robert", "pattinson"));

        String joining = customerList.stream()
                                     .filter(c -> c.getLastName().contains("John"))
                                     .map(c -> c.getFirstName())
                                     .collect(Collectors.joining(", ",
                                            "In customerList: [",
                                            "] have the same lastName John."));

        System.out.println(joining);

    }
}
Output
In customerList: [Peter, Dave] have the same lastName John.

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_Ter_Op_collect_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StreamDemo_Ter_Op_collect_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/07d37f967453de17efa70af5825dc03ae2d5a7c0/BasicJava/StreamDemo_Ter_Op_collect_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment