我有流:

Stream.of(coachingAreas)
            .map(coachingArea -> Observable.from(coachingArea.weeks)
            .map(coachingWeekRef -> coachingMap.put(coachingWeekRef.id, coachingWeekRef.version)));


每个数组都有数据且不为空,但此行:coachingMap.put(coachingWeekRef.id, coachingWeekRef.version)
不被调用。知道为什么吗?

最佳答案

您尚未在
因此,流保持空闲状态。中间操作由终端操作驱动。本质上,终端操作会产生非流结果,例如原始值,对象,集合等。
引入副作用时,请勿在coachingMap.put中间操作中执行map,而应使用toMap收集器。


假设coachingWeekRef.idcoachingWeekRef.version都是整数,则整个查询如下所示:

Map<Integer, Integer> resultSet =
     Stream.of(coachingAreas)
           .map(coachingArea -> Observable.from(coachingArea.weeks))
           .collect(Collectors.toMap(ClassName::getId, ClassName::getVersion));


或使用lambda表达式提取映射键和值,而不是根据需要使用方法引用。

10-05 19:34