Mapping

Java-Mapping

Mapping

Most often it is useful to map the elements of one stream to another. For example, you might want to apply some transformation to the elements in a stream. To do this, you could map the transformed elements to a new stream. Because mapping operations are quite common, the stream API provides built-in support for them. The most general mapping method is map( ) :

Mapping

In the map() method, R specifies the type of elements of the new stream; T is the type of elements of the invoking stream; and fnctn is an instance of Function, which does the mapping. The map function must be stateless. Since a new stream is returned, map( ) is an intermediate method. Function is a functional interface declared in java.util.function, Function<T, R>, T is the element type and R is the result of the mapping. Function declares one abstract method called apply(), R apply(T t), t is a reference to the object being mapped. The mapped result is returned. The following is a simple example program of map(). The program uses map( ) to create a new stream that contains the transformed elements from the original stream.

Program

Mapping

 

The map( ) method is called on the stream returned by arylist.stream( ). Because map( ) is an intermediate operation, its result is a new stream, and that are assigned to istrm and dstrm. The first call to map( ) returns the stream that contains the square of each element from the original stream. The second call to map( ) returns the stream that contains the square root of each element from the original stream.

Another Example

Suppose if you want to create a stream that contains only selected aspects of the original stream. For example, a stream that contains a database of name, telephone, and e-mail address information might map only the e-mail address portion to another stream. Consider the following program, that uses map( ) to create a new stream that contains only selected field from the original stream. In this case, the original stream contains objects of type StudentData, which contains names, roll numbers, and e-mail addresses. The program then maps only the email addresses to a new stream of Email objects. Other informations are discarded.


Program

Mapping pro

Another Example

Because you can pipeline more than one intermediate operation together, you can easily create very powerful actions. For example, the following statement uses filter( ) and then map( ) to produce a new stream that contains only gmail address of specific roll numbers of the elements:

Mapping

This type of filter operation is very common when creating database-style queries. As you gain experience with the stream  API, you will find that such chains of operations can be used to create very sophisticated queries, merges, and selections on a data stream.

Program Source

import java.util.ArrayList;
import java.util.stream.Stream;

public class Javaapp {

    public static void main(String[] args) {

        ArrayList<Integer> arylist = new ArrayList<Integer>();
        arylist.add(10);
        arylist.add(20);
        arylist.add(30);
        arylist.add(40);
        arylist.add(50);
        arylist.add(60);

        System.out.println("Square of the each element :");
        Stream<Integer> istrm = arylist.stream().map((a) -> a * a);
        istrm.forEach((a) -> System.out.println("a-->" + a));

        System.out.println("Square root of the each element :");
        Stream<Double> dstrm = arylist.stream().map((a) -> Math.sqrt(a.doubleValue()));
        dstrm.forEach((a) -> System.out.println("a-->" + a));
    }
}

Program Source

import java.util.ArrayList;
import java.util.stream.Stream;

class StudentData {

    String name;
    int rollnum;
    String email;

    StudentData(String n, int r, String e) {
        name = n;
        rollnum = r;
        email = e;
    }
    
    public String toString(){
        
        return name+"-->"+rollnum+"-->"+email;
    }
}

class Email {

    String email;

    Email(String e) {
        email = e;
    }
}

public class Javaapp {

    public static void main(String[] args) {

        ArrayList<StudentData> arylist = new ArrayList<>();
        arylist.add(new StudentData("Alice",107,"Alice@gmail.com"));
        arylist.add(new StudentData("Lydia",115,"Lydia@gmail.com"));
        arylist.add(new StudentData("Clara",120,"Clara@gmail.com"));
        arylist.add(new StudentData("Julia",132,"Julia@gmail.com"));
        
        System.out.println("-------------All Data------------");
        Stream<StudentData> stdstr = arylist.stream();
        stdstr.forEach((e)->System.out.println("e-->"+e));
        
        System.out.println("-----Mail Only-----");
        Stream<Email> mastr = arylist.stream().map((e)->new Email(e.email));
        mastr.forEach((e)->System.out.println("e-->"+e.email));
    }
}

Leave a Comment