feat: add output format for the data

This commit is contained in:
Anthony Berg 2024-10-15 17:02:02 +02:00
parent 75f5785297
commit cd68d30e6d

View File

@ -8,12 +8,13 @@ public class Main {
// Gets a map with the total occurrences for each domain
Map<String, Integer> domains = getDomainTotal(data);
System.out.println(domains);
// Order the map by descending value of the value
List<Map.Entry<String, Integer>> sortedDomains = sortDomainMapDesc(domains);
System.out.println(sortedDomains);
// Format the data to the format specified
String output = domainListToString(sortedDomains);
System.out.println(output);
}
/**
@ -62,4 +63,25 @@ public class Main {
return sortedDomains;
}
/**
* Creates a string in a format to display the data from a list of domains and their occurrences.
* @param domains A list of all the domains and their occurrences
* @return A string with a specific format
*/
private static String domainListToString(List<Map.Entry<String, Integer>> domains) {
StringBuilder output = new StringBuilder();
for (Map.Entry<String, Integer> entry : domains) {
// Get the values of the entry
String domain = entry.getKey();
Integer occurrences = entry.getValue();
output.append(domain).append(" ").append(occurrences).append("\n");
}
// Remove unnecessary \n at the end of the output
output.deleteCharAt(output.length() - 1);
return output.toString();
}
}