我想删除这个集合的每个最后一个元素.
Set<String> listOfSources = new TreeSet<String>();
for(Route route:listOfRoutes){
Set<Stop> stops = routeStopsService.getStops(route);
for(Stop stop:stops)
listOfSources.add(stop.getStopName());
}
Run Code Online (Sandbox Code Playgroud)
这里我想从listOfSources中删除最后一个元素.
Dan*_*ker 13
您将需要转换回TreeSet,因为Set没有任何订单.
listOfSources.remove( ((TreeSet) listOfSources).last() );
Run Code Online (Sandbox Code Playgroud)
作为替代方案,您可以将listOfSources设置为SortedSet
SortedSet<String> listOfSources = new TreeSet<String>();
Run Code Online (Sandbox Code Playgroud)
然后你可以使用last()方法而不需要转换为TreeSet
listOfSources.remove(listOfSources.last());
Run Code Online (Sandbox Code Playgroud)
我认为这是一种首选方法,因为您认为您的Set有订单.