Java 8 - Stream filter() method example

Posted on January 9, 2017


In this post, we will show you how to filter collection types List and Map using filter() method of java.util.stream.Stream interface.

List + Stream filter() example

Here is an example of using the filter() method of the Stream with a List. The  collect() method of the Stream interface performs a mutable reduction operation on the elements of stream using a Collector.

StreamFilterListExample.java

package com.boraji.tutorial;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author imssbora
 */
public class StreamFilterListExample {
  public static void main(String[] args) {
    List<String> days = Arrays.asList(new String[] {
        "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" 
       });

    // Filter list using old method
    List<String> filteredDays1 = getFilteredDays(days, "T");
    System.out.println(filteredDays1);

    // Filter list using stream filter() method
    List<String> filteredDays2 = getStreamFilteredDays(days, "S");
    System.out.println(filteredDays2);
  }

  private static List<String> getFilteredDays(List<String> days, String str) {
    List<String> list = new ArrayList<>();
    for (String day : days) {
      if (day.startsWith(str)) {
        list.add(day);
      }
    }
    return list;
  }

  private static List<String> getStreamFilteredDays(List<String> days, String str) {
    // Get Stream from list
    Stream<String> stream = days.stream();

    // Filter days list
    List<String> list = stream.filter((day) -> {
      return day.startsWith(str);
    }).collect(Collectors.toList());

    return list;
  }
}

Output

[Tuesday, Thursday]
[Sunday, Saturday]

 

Map + Stream filter() example

Here is an example of using the filter() method of the Stream with a Map.

StreamFilterMapExample.java

package com.boraji.tutorial;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author imssbora
 */
public class StreamFilterMapExample {
  public static void main(String[] args) {
    Map<Integer, String> days = new HashMap<>();
    days.put(1, "Sunday");
    days.put(2, "Monday");
    days.put(3, "Tuesday");
    days.put(4, "Wednesday");
    days.put(5, "Thursday");
    days.put(6, "Friday");
    days.put(7, "Saturday");

    // Old method filtering map
    Map<Integer, String> result1 = getFilteredMap(days, "T");
    System.out.println(result1);

    // Using Stream filter method
    Map<Integer, String> result2 = getStreamFilteredMap(days, "S");
    System.out.println(result2);
  }

  private static Map<Integer, String> getFilteredMap(Map<Integer, String> days, String s) {
    Map<Integer, String> result = new HashMap<>();
    for (Entry<Integer, String> day : days.entrySet()) {
      if (day.getValue().startsWith(s)) {
        result.put(day.getKey(), day.getValue());
      }
    }
    return result;
  }

  private static Map<Integer, String> getStreamFilteredMap(Map<Integer, String> days, String s) {
    // Get Stream from map
    Stream<Entry<Integer, String>> stream = days.entrySet().stream();

    // Filter entry set
    Map<Integer, String> result = stream.filter((e) -> {
      return e.getValue().startsWith(s);
    }).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));

    return result;
  }
}

Output

{3=Tuesday, 5=Thursday}
{1=Sunday, 7=Saturday}