I need to make a function public Collection findEpidemics(List diagnoses, int k, int n) that determines if a disease is an epidemic, and returns a list of the diseases that are epidemic. We say a disease is an epidemic if it occurs more than n times during k consecutive days. The Diagnosis class is public Diagnosis(Disease disease, int day) and has functions getDisease() and getDay().
The list diagnoses looks something like this:
Disease cholera = new Disease("cholera");
Disease dengue = new Disease("dengue");
List<Diagnosis> diagnoses = Arrays.asList(
new Diagnosis(cholera, 0), // registered cholera on day 0
new Diagnosis(cholera, 0),
new Diagnosis(cholera, 1),
new Diagnosis(dengue, 2),
new Diagnosis(dengue, 2),
// we have a gap here
new Diagnosis(cholera, 6),
So what I've done is making a hashmap collect that contains all the occurrences of the diseases for each day so it may for example look like this: {{cholera, 0}=2, {cholera, 1}=1, {dengue, 2}=2, {cholera, 6}=1}
public Collection<String> findEpidemics(List<Diagnosis> diagnoses, int k, int n) {
// first we count occurrences
Map<DiagnosisMetric, Long> collect = diagnoses.stream().
collect(Collectors.groupingBy(DiagnosisMetric::new, counting()));
// check for epidemic
for(int i=0; i<collect.size(); i++){
for(int j=0; j<k; j++){
???
}
}
}
I'm kind of stuck on the part of checking if a disease is an epidemic so I'm stuck on how I can loop over the hashmap and check if the occurrence of a certain disease is more than n for those k consecutive days.