Erstes Element abrufen, das den Kriterien entspricht

Wie erhalte ich das erste Element, das einem Kriterium in einem Stream entspricht? Ich habe es versucht, aber es funktioniert nicht

this.stops.stream().filter(Stop s-> s.getStation().getName().equals(name));

Dieses Kriterium funktioniert nicht, die Filtermethode wird in einer anderen Klasse als Stop aufgerufen.

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;
}

}

Antworten auf die Frage(2)

Ihre Antwort auf die Frage