import java.util.*;
import java.util.stream.*;
public class CountByYearMonth {
public static void main(String[] args) {
List<FileRecord> records = Arrays.asList(
new FileRecord("2025", "07", "Taipei"),
new FileRecord("2025", "07", "Kaohsiung"),
new FileRecord("2025", "07", "Taipei"),
new FileRecord("2025", "06", "Taichung"),
new FileRecord("2025", "06", "Hsinchu")
);
// Group by yyyyMM, then count how many records per group
Map<String, Long> countMap = records.stream()
.collect(Collectors.groupingBy(
r -> r.getYyyy() + r.getMm(), // key: "yyyyMM"
Collectors.counting() // downstream: count elements
));
// If you also want it sorted descending by yyyymm:
LinkedHashMap<String, Long> sortedCountDesc = countMap.entrySet().stream()
.sorted(Map.Entry.<String, Long>comparingByKey(Comparator.reverseOrder()))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(u, v) -> u,
LinkedHashMap::new
));
// Print results
sortedCountDesc.forEach((yyyymm, cnt) ->
System.out.println(yyyymm + " -> " + cnt)
);
}
}