Get startedGet started for free

Filtering and counting emails for customer engagement

A marketing team wants to count the number of customers with company email addresses (emails ending in @company.com) to send promotional offers. Streams allow us to efficiently filter and count these email addresses.

This exercise is part of the course

Input/Output and Streams in Java

View Course

Exercise instructions

  • Convert the emails list into a Stream object.
  • Filter only emails ending with @company.com.
  • Count how many emails match the condition.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

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

public class CompanyEmailFilter {
    public static void main(String[] args) {
        List emails = new ArrayList<>();
        emails.add("[email protected]");
        emails.add("[email protected]");
        emails.add("[email protected]");
        emails.add("[email protected]");

        // Convert list to Stream
        Stream stream = emails.________();
        long count = stream
        	// Filter email ends with "@company.com"
            .________(email -> email.endsWith("@company.com"))
            // Count matching emails
            .________();
        
        System.out.println("Total company emails: " + count);
    }
}
Edit and Run Code