Home  >  Java 8

Java 8 Stream: filter and map Example

By Arvind Rai, November 18, 2014
On this page, we will learn java 8 stream filter and map. These methods are intermediate operations. Filter filters a stream on the basis of a given predicate and returns stream object. And again we can apply other stream methods to this instance. Map is stream function that applies a given function to all its elements and returns the changed stream. Find the method syntax.
Stream<T> filter(Predicate<? super T> predicate) 
We pass predicate as an argument on the basis of which filter method filters the stream and returns the changed stream object.
<R> Stream<R> map(Function<? super T,? extends R> mapper) 
We pass a function as an argument which is applied to all its element of stream and then it returns the changed stream object.

Stream.filter

Stream.filter() filters the calling stream on the basis of given Predicate. In the example we have an employee list and created a Predicate. This Predicate will be passed to filter as an argument. Finally we will print the filtered stream.
FilterDemo.java
package com.concretepage.util.stream;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class FilterDemo {
    public static void main(String[] args) {
        List<Employee> list = Employee.getEmpList();
        Predicate<Employee> filterPredicate = e -> e.id > 1 && e.sal < 6000;
        Consumer<Employee> printConsumer = e -> System.out.println(e.id + ", "+e.sal);
        list.stream().filter(filterPredicate).forEach(printConsumer);
    }
} 
Find the Employee class.
Employee.java
package com.concretepage.util.stream;
import java.util.ArrayList;
import java.util.List;
public class Employee {
    public int id;
    public String name;
    public int sal;
    public Employee(int id,String name,int sal  ){
        this.id = id;
        this.name = name;
        this.sal = sal;
    }
    public static List<Employee> getEmpList(){
        List<Employee> list = new ArrayList<>();
        list.add(new Employee(1, "A", 2000));
        list.add(new Employee(2, "B", 3000));
        list.add(new Employee(3, "C", 4000));
        list.add(new Employee(4, "D", 5000));
        return list;
    }
} 
Output
2, 3000
3, 4000
4, 5000

Stream.map

Stream.map() creates a stream using source stream applying given function. In the example we have an employee stream and then we converted into player stream using map.
MapDemo.java
package com.concretepage.util.stream;
import java.util.List;
import java.util.stream.Stream;
public class MapDemo {
    public static void main(String[] args) {
        List<Employee> list = Employee.getEmpList();
        Stream<Player> players = list.stream().map(e -> new Player(e.id, e.name));
        players.forEach(p -> System.out.println(p.id+ ", "+p.name));
    }
    static class Player {
        int id;
        String name;
        Player(int id, String name){
          this.id = id;
          this.name = name;
        }
    }
} 
Output
1, A
2, B
3, C
4, D
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2018 concretepage.com | Privacy Policy | Contact Us