import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { public static void main(String[] args) { // Get the data from the file List data = ReadFile.open("test.txt"); // Gets a map with the total occurrences for each domain Map domains = getDomainTotal(data); System.out.println(domains); } /** * Totals up the amount of times an email domain occured in the list. * @param emails A list of emails * @return A map of email domains and the total amount of times they occured in the emails parameter */ private static Map getDomainTotal(List emails) { // Create a map of all email address domains and counting them Map domains = new HashMap<>(); for (String email : emails) { if (!EMail.validate(email)) { throw new IllegalArgumentException("There was an invalid email in the file!"); } String domain = EMail.getDomain(email); // Add email domain to the list if (!domains.containsKey(domain)) { // Creates new entry in the map if the domain didn't exist before domains.put(domain, 1); } else { // Updates the map with the new total for the domain // Gets the old total and adds 1 Integer newTotal = domains.get(domain) + 1; // Update domain value with new total domains.replace(domain, newTotal); } } return domains; } }