private static Collection<Container> keepOnlyLastIndexedContainer(Collection<Container> existingContainers, final String partiallyApplied) {

        Collection<Container> result = new ArrayList<>(existingContainers);

        // No index place holder, so nothing to filters
        if (!partiallyApplied.contains(INDEX_PLACEHOLDER)) {
            return result;
        }

        Map<String,Container> containerMap = existingContainers.stream().collect(Collectors.toMap(Container::getName, Function.identity()));

        Container last = null;
        for (long i = 1; i < Long.MAX_VALUE; i++) {
            final String withIndexApplied = partiallyApplied.replaceAll(INDEX_PLACEHOLDER, String.valueOf(i));
            Container mapped = containerMap.get(withIndexApplied);
            if (mapped != null) {
                result.remove(mapped);
                last = mapped;
            } else {
                // Readd last one removed (if any)
                if (last != null) {
                    result.add(last);
                }
                return result;
            }
        }
        throw new IllegalStateException("Internal error: Cannot find a free container index slot in " + existingContainers);
    }
Keep only the entry with the higest index if an indexed naming scheme for container has been chosen or if the container name
pattern doesn't contain any index placeholders then filter containers (analog
{@link #formatContainerName(ImageConfiguration, String, Date, Collection)} but with stopping them in mind).<br>
<br>
The placeholders of the containerNamePattern are resolved as follows:<br>
<ul>
<li>%a is replaced with the image alias</li>
<li>%n is replaced with the image name</li>
<li>%t is replaced with any date (regex \d{10,})</li>
<li>%i is replaced with any number (regex \d+)</li>
</ul>
@param image the image from which to the the container pattern
@param buildTimestamp the timestamp for the build
@param containers the list of existing containers
@return filtered container instances, maybe empty but never null
src/main/java/io/fabric8/maven/docker/util/ContainerNamingUtil.java