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.
Este exercício faz parte do curso
Input/Output and Streams in Java
Instruções do exercício
- Convert the emailslist into aStreamobject.
- Filter only emails ending with @company.com.
- Count how many emails match the condition.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
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);
    }
}