You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何访问List<CalendarDto>列表中嵌套EventDto的place属性值

问题原因
  • Stream的findFirst()是无参方法,你传入eventDto.getPlace()作为参数会直接触发编译错误
  • 你没有做CalendarDto到内部EventDto的映射,且单独实例化的EventDto是空白空对象,调用它的getPlace()拿不到预期值,这个对象完全冗余可以直接删除
正确实现代码

无空校验版本(确定列表非空、所有CalendarDto的event属性都不为空时使用)

final List<CalendarDto> calendarList = calendarService.getCalendars(startDate, endDate);
EventSender sender = new EventSender ("EVENTS");
sender.withData("place", 
    calendarList.stream()
        .map(CalendarDto::getEvent)
        .findFirst()
        .get()
        .getPlace()
);

带空校验兼容版本(异常场景自动返回默认固定值OFFICE,避免空指针)

final List<CalendarDto> calendarList = calendarService.getCalendars(startDate, endDate);
EventSender sender = new EventSender ("EVENTS");
String place = calendarList.stream()
        .map(CalendarDto::getEvent)
        .findFirst()
        .map(EventDto::getPlace)
        .orElse("OFFICE");
sender.withData("place", place);

内容的提问来源于stack exchange,提问作者Rakid Naomi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 01:39:05