Hi I have a stream Collection and I sorted it by the date creation, I need to remove all the elements from this Collections, but less the final one:
This is my code:
List<com.spotify.docker.client.messages.Image> response = dockerClient.listImages()
.stream()
.filter(image -> image.labels() != null && image.labels().containsKey("wantedLabel"))
.sorted((o1, o2) -> o2.created().compareTo(o1.created()))
.collect(
Collectors.toList());
In this list I have my elements sorted by created date and I need to remove all the elements less the final one.
i tried something like:
if (response.stream().iterator().hasNext()) {
response.remove(count);
count++;
}
But I wanted to have something more sophisticated, thanks!
It seems that an image with the latest
created
date needs to be retrieved, thereforeCollectors::maxBy
could be used instead of sorting the list and removing unneeded elements:If it is really needed to have a
List
as result:Update
As soon as the latest image is found, it is possible to use
forEach
to delete the images from the Docker container if necessary: