CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Input/Output and Streams in Java

Afficher le cours

Instructions

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

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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);
    }
}
Modifier et exécuter le code