Complete the code to define a custom annotation for validation.
public @interface [1] { String message() default "Invalid value"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; }
The annotation name should be descriptive and typically starts with 'Valid' for validators. Here, ValidCustom is a good choice.
Complete the code to add the correct meta-annotation to define a custom validator annotation.
@[1](validatedBy = CustomValidator.class) public @interface ValidCustom { String message() default "Invalid value"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; }
The @Constraint annotation is required to specify the validator class for a custom validation annotation.
Fix the error in the validator class by completing the method signature correctly.
public class CustomValidator implements ConstraintValidator<ValidCustom, String> { @Override public boolean [1](String value, ConstraintValidatorContext context) { return value != null && value.matches("^[a-zA-Z]+$"); } }
The method to override in ConstraintValidator is isValid.
Fill both blanks to specify where the custom annotation can be applied and its retention policy.
@Target([1]) @Retention([2]) @Constraint(validatedBy = CustomValidator.class) public @interface ValidCustom { String message() default "Invalid value"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; }
The annotation should be applied to fields, so ElementType.FIELD is correct. The retention must be RUNTIME so it is available during runtime for validation.
Fill all three blanks to create a map of field names to their lengths for words longer than 3 characters.
Map<String, Integer> lengths = words.stream()
.filter(word -> word.length() [1] 3)
.collect(Collectors.toMap(
word -> word.[2](),
word -> word.[3]()
));The filter checks for words longer than 3 characters using '>'. The key mapper uses 'length' (assuming a method to get a key, but here it should be word itself or word.toString(), but for this exercise we accept 'length' as a placeholder). The value mapper uses 'length' to get the length of the word.