MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Input/Output and Streams in Java

Lihat Kursus

Petunjuk latihan

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

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

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 dan Jalankan Kode