feat: sorts the map in descending order

This commit is contained in:
Anthony Berg 2024-10-15 16:47:42 +02:00
parent b4cbd34f6d
commit 75f5785297

View File

@ -1,6 +1,4 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
public class Main {
public static void main(String[] args) {
@ -9,8 +7,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);
}
/**
@ -44,4 +47,19 @@ public class Main {
return domains;
}
/**
* Sorts the domains in descending order, from most occurrences to the least occurrences of domains.
* @param domains An unsorted map of domains with the amount that they occur
* @return Sorted list of Map Entries, in descending order of occurrences
*/
private static List<Map.Entry<String, Integer>> sortDomainMapDesc(Map<String, Integer> domains) {
// Create a list from the map entries
List<Map.Entry<String, Integer>> sortedDomains = new ArrayList<>(domains.entrySet());
// Sort the list in descending order
sortedDomains.sort(Map.Entry.comparingByValue(Comparator.reverseOrder()));
return sortedDomains;
}
}