Returning container object from a filter rxjava

问题: I have a list TeamMember object: class TeamMember { List<Sport> sports; } The task at hand is to return the team member which have an input sport. I have a...

问题:

I have a list TeamMember object:

class TeamMember {
    List<Sport> sports;
}

The task at hand is to return the team member which have an input sport.

I have achieved just this:

Observable.fromIterable(getTeamMembers())
    .flatMapIterable(teamMember -> teamMember.getSports())
    .filter(sport -> sport.equals(selectedSport))

However this would just returned a matched sport and my original team member has been lost.

ideas?


回答1:

Nest it inside one more flatMap and you will get access to TeamMember in lambda.

Observable.fromIterable(getTeamMembers())
        .flatMap(teamMember -> Observable.fromIterable(teamMember.getSports())
                .filter(sport -> sport.equals(selectedSport))
                .map(sportMatchingSelectedSport -> new TeamMemberWithSport(teamMember, sportMatchingSelectedSport)))
  • 发表于 2018-12-31 19:30
  • 阅读 ( 159 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除