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().getName().equals(name)); That criteria is not working, the filter method is invoked in an other class than Stop.
public class Train { private final String name; private final SortedSet<Stop> stops; public Train(String name) { this.name = name; this.stops = new TreeSet<Stop>(); } public void addStop(Stop stop) { this.stops.add(stop); } public Stop getFirstStation() { return this.getStops().first(); } public Stop getLastStation() { return this.getStops().last(); } public SortedSet<Stop> getStops() { return stops; } public SortedSet<Stop> getStopsAfter(String name) { // return this.stops.subSet(, toElement); return null; } } import java.util.ArrayList; import java.util.List; public class Station { private final String name; private final List<Stop> stops; public Station(String name) { this.name = name; this.stops = new ArrayList<Stop>(); } public String getName() { return name; } }