Сделайте это следующим образом:
Map<LocalDate, Map<String, SingleDay>> result = list.stream()
.collect(Collectors.groupingBy(SingleDay::getDate, Collectors.toMap(SingleDay::getCounty, e -> e)));
Демо:
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class SingleDay {
private LocalDate date;
private String County;
public SingleDay(LocalDate date, String county) {
this.date = date;
County = county;
}
public LocalDate getDate() {
return date;
}
public String getCounty() {
return County;
}
@Override
public String toString() {
return "SingleDay [date=" + date + ", County=" + County + "]";
}
// otherstuff
}
public class Main {
public static void main(String[] args) {
List<SingleDay> list = List.of(
new SingleDay(LocalDate.now(), "X"),
new SingleDay(LocalDate.now(), "Y"),
new SingleDay(LocalDate.now(), "Z"),
new SingleDay(LocalDate.now().plusDays(1), "A"),
new SingleDay(LocalDate.now().plusDays(1), "B"),
new SingleDay(LocalDate.now().plusDays(1), "C"));
Map<LocalDate, Map<String, SingleDay>> result = list.stream()
.collect(Collectors.groupingBy(SingleDay::getDate, Collectors.toMap(SingleDay::getCounty, e -> e)));
// Display
result.forEach((k, v) -> System.out.println("Key: " + k + ", Value: " + v));
}
}
Вывод:
Key: 2020-05-27, Value: {A=SingleDay [date=2020-05-27, County=A], B=SingleDay [date=2020-05-27, County=B], C=SingleDay [date=2020-05-27, County=C]}
Key: 2020-05-26, Value: {X=SingleDay [date=2020-05-26, County=X], Y=SingleDay [date=2020-05-26, County=Y], Z=SingleDay [date=2020-05-26, County=Z]}