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 filter and count these email addresses efficiently.

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.

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