void (TemplateImpl template) { Set<String> segments = new HashSet<>(); for (int i = 0; i < template.getSegmentsCount(); i++) { segments.add(template.getSegmentName(i)); } for (int i = template.getVariableCount() - 1; i >= 0; i--) { String varName = template.getVariableNameAt(i); if (!segments.contains(varName)) { template.removeVariable(i); } else { segments.remove(varName); } } }