edi233
edi233

Reputation: 3031

Stream is not called in android

I have stream:

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

every array has data and is not empty, but this line: coachingMap.put(coachingWeekRef.id, coachingWeekRef.version) is not called. Any idea why?

Upvotes: 1

Views: 32

Answers (1)

Ousmane D.
Ousmane D.

Reputation: 56443

  1. You haven't performed a terminal operation on the stream for that reason the stream remains idle. intermediate operations are driven by terminal operations. Essentially terminal operations produce a non-stream result such as a primitive value, an object, a collection etc.

  2. don't do coachingMap.put within the map intermediate operation as you're introducing a side-effect, rather use the toMap collector.

Assuming both coachingWeekRef.id and coachingWeekRef.version are integers then the whole query would look like this:

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

or use a lambda expression to extract the map key and value rather than a method reference if you prefer.

Upvotes: 1

Related Questions