Fetch first element which matches criteria

后端 未结 3 1464
执念已碎
执念已碎 2020-12-01 05:09

How to get first element that matches a criteria in a stream? I\'ve tried this but doesn\'t work

this.stops.stream().filter(Stop s-> s.getStation().getNam         


        
3条回答
  •  醉话见心
    2020-12-01 05:21

    I think this is the best way:

    this.stops.stream().filter(s -> Objects.equals(s.getStation().getName(), this.name)).findFirst().orElse(null);
    

提交回复
热议问题